/*
 * Ingenic IMP emulation FrameSource.
 *
 * Copyright (C) 2014 Ingenic Semiconductor Co.,Ltd
 * Author: Niky <xianghui.shen@ingenic.cn>
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include <linux/v4l2-mediabus.h>

#include <constraints.h>
#include <imp/imp_log.h>
#include <imp/imp_system.h>
#include <imp/imp_utils.h>
#include <imp/imp_encoder.h>

#include <system/system.h>
#include <isp/demosaic.h>
#include <system/imp_v4l2.h>
#include <video/encoder.h>
#include <system/imp_alloc.h>
#include <system/imp_error.h>
#include "../faststart_config.h"
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif
#define TAG "IMP-ISP"

typedef struct {
	uint32_t version;
	int vinum;
	IMPISPAeInitAttr AeInitAttr;
} IMPISPAeInitAttrSelf;

typedef struct {
	uint32_t version;
	int vinum;
	IMPISPAeInfo Aeinfo;
} __attribute__((packed, aligned(1))) IMPISPAEInfoSelf;

typedef struct {
	uint32_t version;
	int vinum;
	IMPISPAeAttr AeAttr;
} IMPISPAeAttrSelf;

static int ae_algo_en = 0;
static pthread_t ae_algo = -1;
static IMPISPAeInitAttrSelf ae_init_info;
static void *priv_data_self = NULL;
IMPISPAeAlgoFunc *ae_func_tmp = NULL;
void IMP_ISP_SetAeAlgoFunc_close(void);
int32_t IMP_ISP_SetAeAlgoFunc_internal(IMPISPAeAlgoFunc *ae_func);

typedef struct {
	uint32_t version;
	int vinum;
	IMPISPAwbInfo Awbinfo;
} __attribute__((packed, aligned(1))) IMPISPAWBInfoSelf;

typedef struct {
	uint32_t version;
	int vinum;
	IMPISPAwbAttr AwbAttr;
} IMPISPAwbAttrSelf;

static int awb_algo_en = 0;
static pthread_t awb_algo = -1;
static void *awb_priv_data_self = NULL;
IMPISPAwbAlgoFunc *awb_func_tmp = NULL;
void IMP_ISP_SetAwbAlgoFunc_close(void);
int32_t IMP_ISP_SetAwbAlgoFunc_internal(IMPISPAwbAlgoFunc *awb_func);

#if ZRT_Platform
/*For Zeratul*/
unsigned int g_sensor_width = 0;
unsigned int g_sensor_height = 0;
/*end*/
#endif

extern int set_framesource_fps(uint32_t fps_num, uint32_t fps_den);
extern int set_framesource_changewait_cnt(void);
static unsigned char custom_sharpness[2] = {128, 128};
static unsigned char custom_contrast[2] = {128, 128};
static int global_mode = DEMOSAIC_ISP_MODE_CUSTOM;

typedef struct {
	uint32_t width;
	uint32_t height;
	uint32_t sta_y_block_size;
	uint32_t sta_y_stride;
	uint32_t sta_y_buf_size;
}IMPISPNcuInfo;

typedef enum {
	ISPDEV_STATE_CLOSE,
	ISPDEV_STATE_OPEN,
	ISPDEV_STATE_RUN,
} ISPDevState;

typedef struct {
	IMPISPTuningOpsMode wdr_attr;
	IMPISPAntiflickerAttr antiflicker[2];

	unsigned char brightness[2];
	unsigned char contrast[2];
	unsigned char saturation[2];
	unsigned char sharpness[2];
	unsigned int fps_num;
	unsigned int fps_den;

	/* test interface */
	IMPISPTuningOpsMode isp_process;
	IMPISPTuningOpsMode fw_freeze;

	IMPISPMaskBlockAttr mask_attr[2][3*4];
	IMPISPDrawBlockAttr draw_attr[2][20];
} IMPISPTuningDev;

/* save the all parameters */
int IMP_ISP_Tuning_SaveAllParam(char *path);

#define MEM_DEV "/dev/mem"
#define JZ_ISP_BASE 0x13380000
#define JZ_ISP_SIZE 0x1b000
typedef struct {
	/* sensor ops */
	char vi_file[MAX_NAME_LEN];
	int vi_fd;
	ISPDevState vi_state;
	IMPSensorInfo active_sensor;

	/* isp tuning */
	char tuning_file[MAX_NAME_LEN];
	int tuning_fd;
	IMPISPTuningDev *tuning_dev;
	int mem_fd;
	void *mapaddr;
	ISPDevState tuning_state;
	void *ncubuf[2];
	int wdr_en;
	void *wdrbuf;
	IMPISPAeAlgoFunc ae_func;
	IMPISPAwbAlgoFunc awb_func;
} IMPISPDev;

typedef struct _sytem_tab{
	uint8_t global_freeze_firmware ;
	uint8_t	 global_manual_exposure ;
	uint8_t	 global_manual_exposure_ratio ;
	uint8_t	 global_manual_integration_time ;
	uint8_t	 global_manual_sensor_analog_gain ;
	uint8_t	 global_manual_sensor_digital_gain ;
	uint8_t	 global_manual_isp_digital_gain ;
	uint8_t	 global_manual_directional_sharpening ;
	uint8_t	 global_manual_un_directional_sharpening ;
	uint8_t	 global_manual_iridix ;
	uint8_t	 global_manual_sinter ;
	uint8_t	 global_manual_temper ;
	uint8_t	 global_manual_awb ;
	uint8_t	 global_antiflicker_enable ;
	uint8_t	 global_slow_frame_rate_enable ;
	uint8_t	 global_manual_saturation ;
	uint32_t  global_manual_exposure_time;
	uint8_t	 global_exposure_dark_target;
	uint8_t	 global_exposure_bright_target;
	uint8_t	 global_exposure_ratio;
	uint8_t	 global_max_exposure_ratio;
	uint16_t  global_integration_time;
	uint16_t  global_max_integration_time;
	uint8_t	 global_sensor_analog_gain;
	uint8_t	 global_max_sensor_analog_gain;
	uint8_t	 global_sensor_digital_gain;
	uint8_t	 global_max_sensor_digital_gain;
	uint8_t	 global_isp_digital_gain;
	uint8_t	 global_max_isp_digital_gain;
	uint8_t	 global_directional_sharpening_target;
	uint8_t	 global_maximum_directional_sharpening;
	uint8_t	 global_minimum_directional_sharpening;
	uint8_t	 global_un_directional_sharpening_target;
	uint8_t	 global_maximum_un_directional_sharpening;
	uint8_t	 global_minimum_un_directional_sharpening;
	uint8_t	 global_iridix_strength_target;
	uint8_t	 global_maximum_iridix_strength;
	uint8_t	 global_minimum_iridix_strength;
	uint8_t	 global_sinter_threshold_target;
	uint8_t	 global_maximum_sinter_strength;
	uint8_t	 global_minimum_sinter_strength;
	uint8_t	 global_temper_threshold_target;
	uint8_t	 global_maximum_temper_strength;
	uint8_t	 global_minimum_temper_strength;
	uint8_t	 global_awb_red_gain;
	uint8_t	 global_awb_blue_gain;
	uint8_t	 global_saturation_target;
	uint8_t	 global_anti_flicker_frequency ;
	uint8_t	 global_ae_compensation;
	uint8_t	 global_calibrate_bad_pixels ;
	uint16_t  global_dis_x ;
	uint16_t  global_dis_y ;
} system_tab;

typedef struct _system_tab_ctrl{
	bool ctrl_global_freeze_firmware ;
	bool ctrl_global_manual_exposure ;
	bool ctrl_global_manual_exposure_ratio ;
	bool ctrl_global_manual_integration_time ;
	bool ctrl_global_manual_sensor_analog_gain ;
	bool ctrl_global_manual_sensor_digital_gain ;
	bool ctrl_global_manual_isp_digital_gain ;
	bool ctrl_global_manual_directional_sharpening ;
	bool ctrl_global_manual_un_directional_sharpening ;
	bool ctrl_global_manual_iridix ;
	bool ctrl_global_manual_sinter ;
	bool ctrl_global_manual_temper ;
	bool ctrl_global_manual_awb ;
	bool ctrl_global_antiflicker_enable ;
	bool ctrl_global_slow_frame_rate_enable ;
	bool ctrl_global_manual_saturation ;
	bool ctrl_global_manual_exposure_time;
	bool ctrl_global_exposure_dark_target;
	bool ctrl_global_exposure_bright_target;
	bool ctrl_global_exposure_ratio;
	bool ctrl_global_max_exposure_ratio;
	bool ctrl_global_integration_time;
	bool ctrl_global_max_integration_time;
	bool ctrl_global_sensor_analog_gain;
	bool ctrl_global_max_sensor_analog_gain;
	bool ctrl_global_sensor_digital_gain;
	bool ctrl_global_max_sensor_digital_gain;
	bool ctrl_global_isp_digital_gain;
	bool ctrl_global_max_isp_digital_gain;
	bool ctrl_global_directional_sharpening_target;
	bool ctrl_global_maximum_directional_sharpening;
	bool ctrl_global_minimum_directional_sharpening;
	bool ctrl_global_un_directional_sharpening_target;
	bool ctrl_global_maximum_un_directional_sharpening;
	bool ctrl_global_minimum_un_directional_sharpening;
	bool ctrl_global_iridix_strength_target;
	bool ctrl_global_maximum_iridix_strength;
	bool ctrl_global_minimum_iridix_strength;
	bool ctrl_global_sinter_threshold_target;
	bool ctrl_global_maximum_sinter_strength;
	bool ctrl_global_minimum_sinter_strength;
	bool ctrl_global_temper_threshold_target;
	bool ctrl_global_maximum_temper_strength;
	bool ctrl_global_minimum_temper_strength;
	bool ctrl_global_awb_red_gain;
	bool ctrl_global_awb_blue_gain;
	bool ctrl_global_saturation_target;
	bool ctrl_global_anti_flicker_frequency ;
	bool ctrl_global_ae_compensation;
	bool ctrl_global_calibrate_bad_pixels ;
	bool ctrl_global_dis_x ;
	bool ctrl_global_dis_y ;
} system_tab_ctrl;

struct isp_core_stab_attr{
	system_tab stab;
	system_tab_ctrl stab_ctrl;
};

struct IMPISPMovestate {
	struct isp_core_stab_attr setting_stab;
	int settingmode;
	unsigned int temper_target_day;
	unsigned int temper_target_night;
	unsigned int sinter_min_day;
	unsigned int sinter_max_day;
	unsigned int sinter_min_night;
	unsigned int sinter_max_night;
	unsigned int sharp_d_min_day;
	unsigned int sharp_d_max_day;
	unsigned int sharp_d_min_night;
	unsigned int sharp_d_max_night;
	unsigned int sharp_ud_min_day;
	unsigned int sharp_ud_max_day;
	unsigned int sharp_ud_min_night;
	unsigned int sharp_ud_max_night;
};

typedef struct {
	char path[64];
} IMPISPBinPath;

int IMP_ISP_Tuning_SetContrast(unsigned char contrast);
int IMP_ISP_Tuning_GetTotalGain(uint32_t *gain);

static IMPISPDev *gISPdev = NULL;
static IMPISPBinPath *bpath = NULL;
static uint32_t g_total_gain = 0;

static struct IMPISPMovestate gmovestate;

static inline IMPISPDev * get_ispdev(void)
{
	return gISPdev;
}

int IMP_ISP_Open(void)
{
	int ret = 0;

	if(gISPdev){
		return 0;
	}

	gISPdev = malloc(sizeof(*gISPdev));
	if (gISPdev == NULL){
		IMP_LOG_ERR(TAG, "Failed to alloc gISPdev!\n");
		return -1;
	}
	memset(gISPdev, 0, sizeof(*gISPdev));

#if 0
	/* open sensor manager fd */
	if(IS_SOC_SERIES(T30)){
		sprintf(gISPdev->vi_file, "/dev/tx-isp");
	}else{
		sprintf(gISPdev->vi_file, "/dev/v4l-subdev0");
	}
#endif
	sprintf(gISPdev->vi_file, "/dev/tx-isp");
	gISPdev->vi_fd = open(gISPdev->vi_file, (O_RDWR | O_CLOEXEC), 0);
	if(gISPdev->vi_fd < 0){
		IMP_LOG_ERR(TAG, "Cannot open %s\n", gISPdev->vi_file);
		return -1;
	}

	gISPdev->vi_state = ISPDEV_STATE_OPEN;

	IMP_LOG_DBG(TAG, "~~~~~~ %s[%d] ~~~~~~~\n", __func__,__LINE__);

	return ret;
}

int IMP_ISP_Close(void)
{
	int ret = 0;

	if(!gISPdev)
		return 0;

	if(gISPdev->vi_state > ISPDEV_STATE_OPEN){
		IMP_LOG_ERR(TAG, "Failed to close, because sensor has been deleted!");
		return -1;
	}

	close(gISPdev->vi_fd);
	free(gISPdev);
	gISPdev = NULL;
	return ret;
}

int32_t IMP_ISP_SetDefaultBinPath(char *path)
{
	int ret = 0;
	IMPISPDev *ispdev = get_ispdev();

	if(ispdev == NULL || path == NULL){
		IMP_LOG_ERR(TAG, "[ %s:%d ] ISPDEV cannot open\n", __func__, __LINE__);
		return -1;
	}

	if(ispdev->vi_state > ISPDEV_STATE_OPEN){
		IMP_LOG_ERR(TAG, "[ %s:%d ] sensor is runing, please call 'emuisp_disablesensor' firstly\n", __func__, __LINE__);
		return -1;
	}

	if(strlen(path) >= 64){
		IMP_LOG_ERR(TAG, "[ %s:%d ] path length exceeds upper limit!!!\n", __func__, __LINE__);
		return -1;
	}

	if(bpath == NULL)
		bpath = (IMPISPBinPath *)malloc(sizeof(IMPISPBinPath));
	sprintf(bpath->path, path);
	IMP_LOG_INFO("Bin file path set successfully.new bin path:%s\n", bpath->path);

	return ret;
}

int32_t IMP_ISP_GetDefaultBinPath(char *path)
{
	int ret = 0;
	IMPISPDev *ispdev = get_ispdev();
	char bpath[64];

	if(ispdev == NULL || path == NULL){
		IMP_LOG_ERR(TAG, "[ %s:%d ] ISPDEV cannot open\n", __func__, __LINE__);
		return -1;
	}

	if(gISPdev->vi_state < ISPDEV_STATE_OPEN){
		IMP_LOG_ERR(TAG, "Sensor is runing, please Call 'EmuISP_DisableSensor' firstly\n");
		return -1;
	}

	if (ioctl(ispdev->vi_fd, VIDIOC_GET_DEFAULT_BIN_PATH, bpath))
		return -1;
	memcpy(path, bpath, sizeof(bpath));
	IMP_LOG_INFO("Bin file path get successfully.bin path:%s", path);

	return ret;
}

extern int32_t ncu_hal_init(char *sensor_name, unsigned int width, unsigned int height);
int IMP_ISP_AddSensor(IMPSensorInfo *info)
{
	int ret = 0;
	IMPISPDev * ispdev = get_ispdev();
	char ncubufname[64] = "";
	struct tx_isp_initarg init;

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}

	if(gISPdev->vi_state > ISPDEV_STATE_OPEN){
		IMP_LOG_ERR(TAG, "Sensor is runing, please Call 'EmuISP_DisableSensor' firstly\n");
		return -1;
	}

	if (ioctl(ispdev->vi_fd, VIDIOC_REGISTER_SENSOR, info)) {
		IMP_LOG_ERR(TAG, "VIDIOC_REGISTER_SENSOR(%s) error!\n", info->name);
		return -1;
	}

	/* set default bin path */
	//printf("sdk bin path:%s[%d]\n", bin_path->path, bin_path);
	if(bpath != NULL)
	{
		ret = ioctl(ispdev->vi_fd, VIDIOC_SET_DEFAULT_BIN_PATH, bpath);
		free(bpath);
		bpath = NULL;
		if(ret)
			return ret;
	}

	/* enum sensor */
	int index = -1;
	struct v4l2_input input;
	input.index = 0;
	while((ret = ioctl(ispdev->vi_fd, VIDIOC_ENUMINPUT, &input)) == 0){
		if(strcmp((const char *)info->name, (const char *)input.name) == 0)
			index = input.index;
		input.index++;
	}
	if(index == -1){
		IMP_LOG_ERR(TAG, "sensor[%s] hasn't been added!\n", info->name);
//		memset(ispdev->current_sensor.name, 0, sizeof(ispdev->current_sensor.name));
		return -1;
	}
	/* select sensor */
	init.vinum = info->sensor_id;
	init.enable = 1;
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_S_INPUT, &init)) != 0){
		IMP_LOG_ERR(TAG, "Failed to select sensor[%s]!\n", info->name);
		return -1;
	}
	memcpy(&ispdev->active_sensor, info, sizeof(IMPSensorInfo));

#if ZRT_Platform
    /*For Zeratul*/
    g_sensor_width = (init.vinum>>16)&0xffff;
    g_sensor_height = init.vinum&0xffff;
#endif

	struct isp_buf_info bufinfo;
	bufinfo.vinum = info->sensor_id;

	if (ioctl(ispdev->vi_fd, VIDIOC_GET_BUF_INFO, &bufinfo)) {
		IMP_LOG_ERR(TAG, "VIDIOC_GET_BUF_INFO() error!\n");
		return -1;
	}
    bufinfo.vinum = info->sensor_id;
	IMP_LOG_INFO(TAG, "%s,%d:vinum = %d, paddr = 0x%x, size = 0x%x\n", __func__, __LINE__,bufinfo.vinum, bufinfo.paddr, bufinfo.size);

	IMPAlloc *alloc = malloc(sizeof(IMPAlloc));
	if (NULL == alloc) {
		printf("error(%s,%d): maloc err\n", __func__, __LINE__);
		return -1;
	}
	sprintf(ncubufname, "ncubuf-%d",bufinfo.vinum);
	ret = IMP_Alloc(alloc, bufinfo.size, ncubufname);
	if (0 != ret) {
		printf("error(%s,%d): IMP_Alloc\n", __func__, __LINE__);
		return -1;
	}
	ispdev->ncubuf[info->sensor_id] = alloc;
	bufinfo.paddr = alloc->info.paddr;
	if (ioctl(ispdev->vi_fd, VIDIOC_SET_BUF_INFO, &bufinfo)) {
		IMP_LOG_ERR(TAG, "VIDIOC_SET_BUF_INFO() error!\n");
		return -1;
	}

	return 0;
}

IMPAlloc *IMP_ISP_Tuning_GetNCUAlloc()
{
	IMPISPDev * ispdev = get_ispdev();

	return ispdev ? ispdev->ncubuf[0] : NULL;
}

int IMP_ISP_EnableSensor(void)
{
	int ret = 0;
	IMPISPDev * ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}

	if(ae_algo_en == 1)
		IMP_ISP_SetAeAlgoFunc_internal(ae_func_tmp);

	if(awb_algo_en == 1)
		IMP_ISP_SetAwbAlgoFunc_internal(awb_func_tmp);

	/* enum sensor */
	int index = -1;

	/* get current sensor */
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_G_INPUT, &index)) != 0){
		IMP_LOG_ERR(TAG, "Failed to Get sensor info!\n");
		return -1;
	}

	if(index == -1){
		IMP_LOG_ERR(TAG, "sensor hasn't been added!\n");
		return -1;
	}

	if ((ret = ioctl(ispdev->vi_fd, VIDIOC_STREAMON, NULL)) != 0) {
		IMP_LOG_ERR(TAG, "Failed to enable sensor!\n");
		return -1;
	}

	index = 0;
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_CREATE_SUBDEV_LINKS, &index)) != 0){
		IMP_LOG_ERR(TAG, "Failed to Get sensor info!\n");
		return -1;
	}

	if ((ret = ioctl(ispdev->vi_fd, VIDIOC_LINKS_STREAMON, NULL)) != 0) {
		IMP_LOG_ERR(TAG, "Failed to enable sensor!\n");
		return -1;
	}
	gISPdev->vi_state += ISPDEV_STATE_RUN;
	return 0;
}

int IMP_ISP_DisableSensor(void)
{
	int ret = 0;
	IMPISPDev * ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}
	/* enum sensor */
	int index = -1;

	/* get current sensor */
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_G_INPUT, &index)) != 0){
		IMP_LOG_ERR(TAG, "Failed to Get sensor info!\n");
		return -1;
	}

	if(index == -1){
		IMP_LOG_ERR(TAG, "sensor hasn't been added!\n");
		return -1;
	}

	if(ae_algo_en == 1){
		IMP_ISP_SetAeAlgoFunc_close();
	}

	if(awb_algo_en == 1){
		IMP_ISP_SetAwbAlgoFunc_close();
	}

	if ((ret = ioctl(ispdev->vi_fd, VIDIOC_LINKS_STREAMOFF, NULL)) != 0) {
		IMP_LOG_ERR(TAG, "Failed to disable sensor!\n");
		return -1;
	}

	index = -1;
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_DESTROY_SUBDEV_LINKS, &index)) != 0){
		IMP_LOG_ERR(TAG, "Failed to Get sensor info!\n");
		return -1;
	}

	if ((ret = ioctl(ispdev->vi_fd, VIDIOC_STREAMOFF, NULL)) != 0) {
		IMP_LOG_ERR(TAG, "Failed to disable sensor!\n");
		return -1;
	}
	gISPdev->vi_state -= ISPDEV_STATE_RUN;

	return 0;
}

int IMP_ISP_DelSensor(IMPSensorInfo *info)
{
	int ret = 0;
	IMPISPDev * ispdev = get_ispdev();
	struct tx_isp_initarg init;

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}

	if(gISPdev->vi_state > ISPDEV_STATE_OPEN){
		IMP_LOG_ERR(TAG, "Sensor is runing, please Call 'EmuISP_DisableSensor' firstly\n");
		return -1;
	}

	/* clear isp input */
	init.vinum = info->sensor_id;
	init.enable = 0;
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_S_INPUT, &init)) != 0){
		IMP_LOG_ERR(TAG, "Failed to select sensor[%s]!\n", info->name);
		return -1;
	}

	if (ioctl(ispdev->vi_fd, VIDIOC_RELEASE_SENSOR, info)) {
		IMP_LOG_ERR(TAG, "VIDIOC_REGISTER_SENSOR(%s) error!\n", info->name);
		return -1;
	}
	memset(&ispdev->active_sensor, 0, sizeof(ispdev->active_sensor));

	if (ispdev->ncubuf[info->sensor_id]) {
		IMP_Free(ispdev->ncubuf[info->sensor_id], (void *)((IMPAlloc*)(ispdev->ncubuf[info->sensor_id]))->info.vaddr);
		free(ispdev->ncubuf[info->sensor_id]);
		ispdev->ncubuf[info->sensor_id] = NULL;
    }

	return 0;
}

int IMP_ISP_SetSensorRegister(uint32_t index, uint32_t value)
{
	int ret = 0;
	struct v4l2_dbg_register reg;
	IMPSensorInfo *info = NULL;
	IMPISPDev * ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}

	if(gISPdev->vi_state < ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "Sensor doesn't Run!\n");
		return -1;
	}
	info = &ispdev->active_sensor;
	if(info->cbus_type == 0){
		IMP_LOG_ERR(TAG, "There isn't sensor!\n");
		return -1;
	}
	if(info->cbus_type == TX_SENSOR_CONTROL_INTERFACE_I2C){
		reg.match.type = V4L2_CHIP_MATCH_I2C_DRIVER;
	}else{
		IMP_LOG_ERR(TAG, "Don't support spi sensor!\n");
		return -1;
	}
	memcpy(reg.match.name, &info->name, sizeof(reg.match.name));
	reg.reg = index;
	reg.val = value;
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_DBG_S_REGISTER,&reg)) !=0 ){
		printf("sorry,g_register failed!\n");
	}
	return ret;
}

int IMP_ISP_GetSensorRegister(uint32_t index, uint32_t *value)
{
	int ret = 0;
	struct v4l2_dbg_register reg;
	IMPSensorInfo *info = NULL;
	IMPISPDev * ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}

	if(gISPdev->vi_state < ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "Sensor doesn't Run!\n");
		return -1;
	}
	info = &ispdev->active_sensor;
	if(info->cbus_type == 0){
		IMP_LOG_ERR(TAG, "There isn't sensor!\n");
		return -1;
	}
	if(info->cbus_type == TX_SENSOR_CONTROL_INTERFACE_I2C){
		reg.match.type = V4L2_CHIP_MATCH_I2C_DRIVER;
	}else{
		IMP_LOG_ERR(TAG, "Don't support spi sensor!\n");
		return -1;
	}
	memcpy(reg.match.name, &info->name, sizeof(reg.match.name));
	reg.reg = index;
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_DBG_G_REGISTER,&reg)) !=0 ){
		printf("sorry,g_register failed!\n");
	}
	*value = (unsigned int)(reg.val);
	return ret;
}


static inline IMPISPTuningDev * get_isptuningdev(void)
{
	if(gISPdev && gISPdev->tuning_dev)
		return gISPdev->tuning_dev;
	return NULL;
}

/* tuning deamon */
#define ISP_TUNING_DEAMON_FUNCS_NUM 10
#define ISP_TUNING_FUNCS_DEBUG_PATH	"/tmp/isp_tuning_func"
#define ISP_TUNING_MOVE_COEFF_PATH	"/etc/sensor/"
struct isp_tuning_deamon_funcs {
	char name[20];
	unsigned int en;
	int (*func)(void *arg);
	void *arg;
};
struct isp_tuning_deamon_info {
	unsigned int thread_en;
	pthread_t tid;
	struct isp_tuning_deamon_funcs funcs[ISP_TUNING_DEAMON_FUNCS_NUM];
	unsigned int funcs_en;
};
static struct isp_tuning_deamon_info g_isp_deamon_info;
static int g_isp_deamon_init = 0;
static pthread_mutex_t g_isp_deamon_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t isp_running_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t isp_systab_mutex = PTHREAD_MUTEX_INITIALIZER;
static void *isp_tuning_deamon_thread(void *arg)
{
	int i = 0;
	int fnum = 0;
	struct isp_tuning_deamon_funcs *f;
	FILE *fp;
	prctl(PR_SET_NAME, __func__);
	while(1) {
		pthread_mutex_lock(&g_isp_deamon_mutex);
		if (0 == g_isp_deamon_info.thread_en) {
			goto done;
		}

		fp = fopen(ISP_TUNING_FUNCS_DEBUG_PATH,"r");
		if(fp){
			fscanf(fp,"%x",&g_isp_deamon_info.funcs_en);
			fclose(fp);
		}
		fnum = 0;
		for (i = 0; i < ISP_TUNING_DEAMON_FUNCS_NUM; i++) {
			f = &g_isp_deamon_info.funcs[i];
			if ((f->en)&&(NULL!=f->func) && (g_isp_deamon_info.funcs_en & (1 << i))) {
				//IMP_LOG_INFO(TAG, "%s,%d func:%s\n", __func__, __LINE__, f->name);
				f->func(f->arg);
				fnum++;
			}
		}
		if (0 == fnum) {
			IMP_LOG_WARN(TAG, "%s(%d)fnum=0\n", __func__, __LINE__);
			goto done;
		}
		pthread_mutex_unlock(&g_isp_deamon_mutex);
		sleep(1);
	}
done:
	/*
	 * 一个joinable线程里面不能强制将自己存储线程号的变量置为0，因需要使用这个变量来pthread_join来释放线程
	 * ，不然会因线程资源不够而导致后续创建线程失败
	 */
	//g_isp_deamon_info.tid = 0;
	//g_isp_deamon_info.thread_en = 0;
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return 0;
}
static int isp_tuning_deamon_init(void)
{
	int ret = 0;
	pthread_mutex_lock(&g_isp_deamon_mutex);
	if (1 == g_isp_deamon_init) {
		IMP_LOG_ERR(TAG, "err: tuning deamon init already\n");
		goto err_deamon_init;
	}
	memset(&g_isp_deamon_info, 0, sizeof(g_isp_deamon_info));
	g_isp_deamon_init = 1;
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return ret;
err_deamon_init:
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return ret;
}
static int isp_tuning_deamon_deinit(void)
{
	int ret = 0;
	pthread_mutex_lock(&g_isp_deamon_mutex);
	if (0 == g_isp_deamon_init) {
		IMP_LOG_ERR(TAG, "err: tuning deamon deinit already\n");
		goto err_deamon_deinit;
	}
	if ((0 != g_isp_deamon_info.thread_en) && (0 != g_isp_deamon_info.tid)) {
		g_isp_deamon_info.thread_en = 0;
		pthread_mutex_unlock(&g_isp_deamon_mutex);
		ret = pthread_join(g_isp_deamon_info.tid, NULL);
		pthread_mutex_lock(&g_isp_deamon_mutex);
		if (0 != ret) {
			IMP_LOG_ERR(TAG, "err: pthread_join failed\n");
			goto err_pthread_join;
		}
	}
	g_isp_deamon_init = 0;
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return ret;
err_pthread_join:
err_deamon_deinit:
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return ret;
}
static int isp_tuning_deamon_func_add(char *name, int (*func)(void *arg), void *arg)
{
	int i = 0;
	int ret = 0;
	int has = 0;
	struct isp_tuning_deamon_funcs *f;
	if ((NULL == name) || (NULL == func))
		return -1;
	if (strlen(name) > 19) {
		IMP_LOG_ERR(TAG, "err: name too long!\n");
		return -1;
	}
	if (0 == g_isp_deamon_init) {
		IMP_LOG_ERR(TAG, "err:not init!\n");
		return -1;
	}
	pthread_mutex_lock(&g_isp_deamon_mutex);
	has = 0;
	for(i = 0; i < ISP_TUNING_DEAMON_FUNCS_NUM; i++) {
		f = &g_isp_deamon_info.funcs[i];
		if ((f->en)&&(func==f->func)&&(0 == strcmp(f->name, name))) {
			has = 1;
			break;
		}
	}
	if (has) {
		IMP_LOG_ERR(TAG, "warning: %s func has added!\n", name);
		pthread_mutex_unlock(&g_isp_deamon_mutex);
		return 0;

	}
	/* find empty item */
	for(i = 0; i < ISP_TUNING_DEAMON_FUNCS_NUM; i++) {
		f = &g_isp_deamon_info.funcs[i];
		if ((!f->en)&&(NULL==f->func)) {
			int slen = strlen(name);
			if (slen > 19) slen = 19;
			f->en = 1;
			f->func = func;
			strncpy(f->name, name, slen);
			f->arg = arg;
			g_isp_deamon_info.funcs_en |= (f->en << i);
			break;
		}
	}

	if (i >= ISP_TUNING_DEAMON_FUNCS_NUM) {
		pthread_mutex_unlock(&g_isp_deamon_mutex);
		IMP_LOG_ERR(TAG, "err:isp deamon funcs if full!\n");
		return -1;
	} else {
		/* if ((0 == g_isp_deamon_info.thread_en) && (0 == g_isp_deamon_info.tid)) { */
		/* 	ret = pthread_create(&g_isp_deamon_info.tid, NULL, isp_tuning_deamon_thread, NULL); */
		/* 	if (0 != ret) { */
		/* 		g_isp_deamon_info.tid = 0; */
		/* 		IMP_LOG_ERR(TAG, "err: pthread_create failed:ret=%d\n", ret); */
		/* 	} else { */
		/* 		g_isp_deamon_info.thread_en = 1; */
		/* 	} */

		/* } */
	}
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return 0;
}
static int isp_tuning_deamon_func_del(char *name, int (*func)(void *arg))
{
	int i = 0;
	int find_num = 0;
	struct isp_tuning_deamon_funcs *f;
	if ((NULL == name) || (NULL == func))
		return -1;
	if (0 == g_isp_deamon_init) {
		IMP_LOG_ERR(TAG, "err:not init!\n");
		return -1;
	}
	pthread_mutex_lock(&g_isp_deamon_mutex);
	for(i = 0; i < ISP_TUNING_DEAMON_FUNCS_NUM; i++) {
		f = &g_isp_deamon_info.funcs[i];
		if ((f->en)&&(NULL!=f->func)) {
			if ((0 == strcmp(name, f->name)) && (f->func == func)) {
				memset(f, 0, sizeof(struct isp_tuning_deamon_funcs));
				find_num++;
			}
		}
	}
	if (find_num > 1) {
		IMP_LOG_ERR(TAG, "err:find same funcs, and deleted\n");
	}
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return 0;
}

static int isp_tuning_func_contrast_judge(void *arg)
{
	IMPISPTuningDev * tuning = get_isptuningdev();
	IMPISPDev *ispdev = get_ispdev();
	struct v4l2_control control;
	int ret = 0;
	static unsigned int static_gain = 0;

	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(g_total_gain && g_total_gain != static_gain){
		static_gain = g_total_gain;
//		control.id = V4L2_CID_CONTRAST;
		control.value = tuning->contrast[0] | (static_gain << 8);
		ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
	}

	return ret;
}

#define ISP_VIC_FRD_FILE "/proc/jz/isp/isp-w02"
static int isp_video_drop_status = 0;
static int isp_video_drop_notify_c = 0;
static unsigned int isp_vic_frd_c = 0;
static void (*isp_video_drop_func)(void) = NULL;
static int isp_tuning_func_video_drop(void *arg)
{
	int ret = 0;
	FILE *fp = NULL;
	unsigned int vic_c = 0;
	fp = fopen(ISP_VIC_FRD_FILE, "r");
	if (NULL == fp) {
		IMP_LOG_ERR(TAG, "err:fopen %s\n", ISP_VIC_FRD_FILE);
		ret = -1;
		goto err_fopen;
	}
	ret = fscanf(fp,"%d", &vic_c);
	if (1 == ret) {
		if (vic_c == isp_vic_frd_c)	{
			isp_video_drop_status++;
		} else {
			isp_vic_frd_c = vic_c;
			isp_video_drop_notify_c = 0;
		}
		if (isp_video_drop_status >= 2) {
			isp_video_drop_status = 0;
			isp_video_drop_notify_c++;
			if (isp_video_drop_notify_c <= 3) {
				/* do */
				IMP_LOG_ERR(TAG, "err:video drop \n");
				if (isp_video_drop_func) {
					isp_video_drop_func();
				}
			}
		}
	} else {
		IMP_LOG_ERR(TAG, "err:fscanf %s\n", ISP_VIC_FRD_FILE);
		ret = -1;
		goto err_fscanf;
	}
	fclose(fp);
	return 0;

err_fscanf:
	fclose(fp);
err_fopen:
	return ret;
}

static int isp_tuning_func_video_drop_set(void (*func)(void))
{
	pthread_mutex_lock(&g_isp_deamon_mutex);
	isp_video_drop_func = func;
	pthread_mutex_unlock(&g_isp_deamon_mutex);
	return 0;
}

static int isp_tuning_func_update_total_gain(void *arg)
{
	uint32_t tmp_gain = 0;

	if (IMP_ISP_Tuning_GetTotalGain(&tmp_gain) < 0) {
		g_total_gain = 0;
		return -1;
	}

	g_total_gain = tmp_gain;

	return 0;
}

#if 0
static int isp_tuning_deamon_dump(void)
{
	int i = 0;
	for (i = 0; i < ISP_TUNING_DEAMON_FUNCS_NUM; i++) {
		struct isp_tuning_deamon_funcs *f;
		f = &g_isp_deamon_info.funcs[i];
		printf("%22s %d 0x%x\n", f->name, f->en, (unsigned int)f->func);
	}
	return 0;
}
#endif
uint32_t isp_get_total_gain(void)
{
	return g_total_gain;
}

int IMP_ISP_EnableTuning(void)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev *tdev = NULL;
	struct isp_image_tuning_default_ctrl def_ctrl;
	char funcs_en[32];
	FILE *fp;
	int ret = 0;
	int tmp = 0;
	unsigned char value;
	char paths[32];

	if(ispdev == NULL)
		return -1;
	tdev = ispdev->tuning_dev;

	if(tdev != NULL)
		return 0;

	/* open isp tuning fd */
#if 0
	if(IS_SOC_SERIES(T30)){
		sprintf(ispdev->tuning_file, "/dev/isp-m0");
	}else{
		sprintf(ispdev->tuning_file, "/dev/video0");
	}
#endif

	sprintf(ispdev->tuning_file, "/dev/isp-m0");
	ispdev->tuning_fd = open(ispdev->tuning_file, (O_RDWR | O_CLOEXEC), 0);
	if(ispdev->tuning_fd < 0){
		IMP_LOG_ERR(TAG, "Cannot open %s\n", ispdev->tuning_file);
		return -1;
	}
	tdev = malloc(sizeof(*tdev));
	if(tdev == NULL){
		close(ispdev->tuning_fd);
		return -1;
	}
	memset(tdev, 0, sizeof(*tdev));
	ispdev->tuning_dev = tdev;
	ispdev->tuning_state = ISPDEV_STATE_RUN;

	/* Open the file of ISP device, then set/obtain a register */
	ispdev->mem_fd = open(MEM_DEV, O_RDWR | O_SYNC);
	if(ispdev->mem_fd <= 0) {
		IMP_LOG_ERR(TAG, "Failed to open %s\n", MEM_DEV);
	}else{
		ispdev->mapaddr = mmap(NULL, JZ_ISP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, ispdev->mem_fd, JZ_ISP_BASE);
		if(ispdev->mapaddr == NULL) {
			IMP_LOG_ERR(TAG, "Failed to mmap isp base addr\n");
		}
	}

	/* get default fps of sensor */
	def_ctrl.vinum = 0;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.control.id = IMAGE_TUNING_CID_CONTROL_FPS;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		tmp = def_ctrl.control.value;
		tdev->fps_num = (tmp >> 16) & 0xffff;
		tdev->fps_den = tmp & 0xffff;
		set_framesource_fps(tdev->fps_num, tdev->fps_den);
	}


	/** configure the default value	**/
	tdev->contrast[0] = custom_contrast[0];
	tdev->contrast[1] = custom_contrast[1];
	IMP_ISP_Tuning_GetSharpness(&value);
	IMP_ISP_Tuning_GetBrightness(&value);
	IMP_ISP_Tuning_GetSaturation(&value);

	isp_tuning_deamon_init();
	isp_tuning_deamon_func_add("update_total_gain", isp_tuning_func_update_total_gain, NULL); /* This guy run first */
	isp_tuning_deamon_func_add("contrastjudge", isp_tuning_func_contrast_judge, NULL);
#if 1
	isp_tuning_deamon_func_add("videodrop", isp_tuning_func_video_drop, NULL);
#endif

	sprintf(funcs_en,"0x%08x\n", g_isp_deamon_info.funcs_en);
	fp = fopen(ISP_TUNING_FUNCS_DEBUG_PATH,"w+");
	if(fp){
		fwrite(funcs_en,1,sizeof(funcs_en),fp);
		fclose(fp);
	}
	/* When sensor is moving, the tunning paramter is as follow. */
	memset(&gmovestate, 0, sizeof(gmovestate));
	memset(paths, 0, sizeof(paths));
	sprintf(paths,"%s%smove.txt", ISP_TUNING_MOVE_COEFF_PATH, ispdev->active_sensor.name);
	fp = fopen(paths,"r");
	if(fp){
		/*fscanf(fp,"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", &gmovestate.temper_target_day, &gmovestate.temper_target_night,*/
		fscanf(fp,"%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", &gmovestate.temper_target_day, &gmovestate.temper_target_night,
		       &gmovestate.sinter_min_day, &gmovestate.sinter_max_day, &gmovestate.sinter_min_night, &gmovestate.sinter_max_night,
		       &gmovestate.sharp_d_min_day, &gmovestate.sharp_d_max_day, &gmovestate.sharp_d_min_night, &gmovestate.sharp_d_max_night,
		       &gmovestate.sharp_ud_min_day, &gmovestate.sharp_ud_max_day, &gmovestate.sharp_ud_min_night, &gmovestate.sharp_ud_max_night);
		/*printf("t_day = %d,sd_min_night=%d,sud_min_night=%d\n", gmovestate.temper_target_day, gmovestate.sharp_d_min_night, gmovestate.sharp_ud_min_night);*/
		fclose(fp);
	}

	return 0;
}

int IMP_ISP_DisableTuning(void)
{
	IMPISPDev *ispdev = get_ispdev();

	if(ispdev == NULL)
		return 0;
	if(ispdev->tuning_dev){
		free(ispdev->tuning_dev);
		ispdev->tuning_dev = NULL;
		close(ispdev->tuning_fd);
	}

	if(ispdev->mapaddr){
		munmap((void *)ispdev->mapaddr, JZ_ISP_SIZE);
		close(ispdev->mem_fd);
		ispdev->mapaddr = NULL;
		ispdev->mem_fd = 0;
	}

	ispdev->tuning_state = ISPDEV_STATE_CLOSE;

	isp_tuning_deamon_func_del("videodrop", isp_tuning_func_video_drop);
	isp_tuning_deamon_func_del("contrastjudge", isp_tuning_func_contrast_judge);
	isp_tuning_deamon_func_del("update_total_gain", isp_tuning_func_update_total_gain);

	isp_tuning_deamon_deinit();
	return 0;
}

int IMP_ISP_Tuning_SetISPBypass(IMPISPTuningOpsMode enable)
{
	int ret;
	IMPISPDev *ispdev;
	struct v4l2_control control;
	int index = 0;

	ispdev = get_ispdev();
	if(ispdev == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	index = -1;
	if ((ret = ioctl(ispdev->vi_fd, VIDIOC_LINKS_STREAMOFF, NULL)) != 0) {
		IMP_LOG_ERR(TAG, "Failed to DISABLE links!\n");
		return -1;
	}
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_DESTROY_SUBDEV_LINKS, &index)) != 0){
		IMP_LOG_ERR(TAG, "Failed to destroy links!\n");
		return -1;
	}

	control.id = IMAGE_TUNING_CID_CUSTOM_ISP_PROCESS;
	control.value = enable;

	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
	if(ret != 0) {
		IMP_LOG_ERR(TAG, "VIDIOC_S_CTRL error !\n");
		return -1;
	}

	if(enable == IMPISP_TUNING_OPS_MODE_DISABLE){
		index = 1;
	}else{
		index = 0;
	}
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_CREATE_SUBDEV_LINKS, &index)) != 0){
		IMP_LOG_ERR(TAG, "Failed to create links!\n");
		return -1;
	}
	if ((ret = ioctl(ispdev->vi_fd, VIDIOC_LINKS_STREAMON, NULL)) != 0) {
		IMP_LOG_ERR(TAG, "Failed to enable links!\n");
		return -1;
	}

	return 0;
}

int IMP_ISP_Tuning_SetSensorFPS(uint32_t fps_num, uint32_t fps_den)
{
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPAeAlgoFunc *AeAlgoFuncSelf = NULL;
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev *tuning = get_isptuningdev();
	int tmp = (fps_num << 16) | fps_den;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	AeAlgoFuncSelf = &ispdev->ae_func;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_CONTROL_FPS;
	def_ctrl.control.value = tmp;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		tuning->fps_num = fps_num;
		tuning->fps_den = fps_den;
		set_framesource_fps(fps_num, fps_den);
		if(ae_algo_en == 1)
			AeAlgoFuncSelf->notify(priv_data_self, IMPISP_AE_NOTIFY_FPS_CHANGE, NULL);
	}

	return ret;
}

int IMP_ISP_Tuning_GetSensorFPS(uint32_t *fps_num, uint32_t *fps_den)
{
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPTuningDev * tuning = get_isptuningdev();
	IMPISPDev *ispdev;
	int tmp = 0;
	int ret = 0;

	ispdev = get_ispdev();
	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
 
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_CONTROL_FPS;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		tmp = def_ctrl.control.value;
		tuning->fps_num = (tmp >> 16) & 0xffff;
		tuning->fps_den = tmp & 0xffff;
		set_framesource_fps(tuning->fps_num, tuning->fps_den);
		*fps_num = tuning->fps_num;
		*fps_den = tuning->fps_den;
	}

	return ret;
}

int IMP_ISP_Tuning_GetTotalGain(uint32_t *gain)
{
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPTuningDev * tuning = get_isptuningdev();
	IMPISPDev *ispdev;
	int ret = 0;

	ispdev = get_ispdev();
	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_GET_TOTAL_GAIN;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret)
		*gain = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_SetBrightness(unsigned char bright)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), bright = %d\n", __func__, __LINE__, bright);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	control.id = V4L2_CID_BRIGHTNESS;
	control.value = bright;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->brightness[0] = bright;
	}
	return ret;
}

int IMP_ISP_Tuning_GetBrightness(unsigned char *pbright)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	if(pbright == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	control.id = V4L2_CID_BRIGHTNESS;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL, &control);
	if(!ret){
		*pbright = control.value;
		tuning->brightness[0] = control.value;
	};
	return ret;
}

int IMP_ISP_Tuning_SetContrast_internal(unsigned char contrast, DEMOSAIC_ISP_MODE_E mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	unsigned char cat;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), contrast=%d, mode = %d\n", __func__, __LINE__, contrast, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if (ispdev->tuning_state != ISPDEV_STATE_RUN) {
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	if (DEMOSAIC_ISP_MODE_CUSTOM == mode){
		custom_contrast[0] = contrast;
		if (DEMOSAIC_ISP_MODE_ENABLE == global_mode)
			return 0;
		cat = custom_contrast[0];
	} else if (DEMOSAIC_ISP_MODE_ENABLE == mode) {
		global_mode = DEMOSAIC_ISP_MODE_ENABLE;
		cat = contrast;
	} else if (DEMOSAIC_ISP_MODE_DISABLE == mode) {
		cat = custom_contrast[0];
		global_mode = DEMOSAIC_ISP_MODE_CUSTOM;
	} else {
		IMP_LOG_ERR(TAG, "%s(%d), We do not support this mode\n", __func__, __LINE__);
		return -1;
	}

	control.id = V4L2_CID_CONTRAST;
	control.value = cat;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->contrast[0] = cat;
	}
	return ret;
}

int IMP_ISP_Tuning_SetContrast(unsigned char contrast)
{
	int ret = 0;
	ret = IMP_ISP_Tuning_SetContrast_internal(contrast, DEMOSAIC_ISP_MODE_CUSTOM);
	return ret;
}

int IMP_ISP_Tuning_GetContrast(unsigned char *pcontrast)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	/* struct v4l2_control control; */
	int ret = 0;

	if(pcontrast == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
#if 0
	control.id = V4L2_CID_CONTRAST;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL, &control);
	if(!ret){
		*pcontrast = control.value;
	};
#endif
	*pcontrast = custom_contrast[0];
	tuning->contrast[0] = custom_contrast[0];
	return ret;
}

int IMP_ISP_Tuning_SetSharpness_internal(unsigned char sharpness, DEMOSAIC_ISP_MODE_E mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;
	unsigned char sharp;

	//IMP_LOG_INFO(TAG, "%s(%d), sharpness=%d, mode = %d\n", __func__, __LINE__, sharpness, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	if (DEMOSAIC_ISP_MODE_CUSTOM == mode){
		custom_sharpness[0] = sharpness;
		if (DEMOSAIC_ISP_MODE_ENABLE == global_mode)
			return 0;
		sharp = custom_sharpness[0];
	} else if (DEMOSAIC_ISP_MODE_ENABLE == mode) {
		global_mode = DEMOSAIC_ISP_MODE_ENABLE;
		sharp = sharpness;
	} else if (DEMOSAIC_ISP_MODE_DISABLE == mode) {
		sharp = custom_sharpness[0];
		global_mode = DEMOSAIC_ISP_MODE_CUSTOM;
	} else {
		printf("We do not support this mode\n");
		return -1;
	}

	control.id = V4L2_CID_SHARPNESS;
	control.value = sharp;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->sharpness[0] = sharp;
	}

	return ret;
}

int IMP_ISP_Tuning_SetSharpness(unsigned char sharpness)
{
	int ret = 0;

	ret = IMP_ISP_Tuning_SetSharpness_internal(sharpness, DEMOSAIC_ISP_MODE_CUSTOM);
	return ret;
}

int IMP_ISP_Tuning_GetSharpness(unsigned char *psharpness)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	/* struct v4l2_control control; */
	int ret = 0;

	if(psharpness == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

#if 0
	control.id = V4L2_CID_SHARPNESS;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL, &control);
	if(!ret){
		*psharpness = control.value;
	};
#endif
	*psharpness = custom_sharpness[0];
	tuning->sharpness[0] = custom_sharpness[0];

	return ret;
}

int IMP_ISP_Tuning_SetSaturation(unsigned char sat)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), sat = %d\n", __func__, __LINE__, sat);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	control.id = V4L2_CID_SATURATION;
	control.value = sat;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->saturation[0] = sat;
	}
	return ret;
}

int IMP_ISP_Tuning_GetSaturation(unsigned char *psat)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	if(psat == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	control.id = V4L2_CID_SATURATION;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL, &control);
	if(!ret){
		*psat = control.value;
		tuning->saturation[0] = control.value;
	};
	return ret;
}

int IMP_ISP_Tuning_SetISPHflip(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), mode = %d\n", __func__, __LINE__, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = V4L2_CID_HFLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetISPHflip(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = V4L2_CID_HFLIP;
	def_ctrl.control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetISPVflip(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), mode = %d\n", __func__, __LINE__, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = V4L2_CID_VFLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetISPVflip(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = V4L2_CID_VFLIP;
	def_ctrl.control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetSensorHflip(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_H_FLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetSensorHflip(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_H_FLIP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetSensorVflip(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_V_FLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetSensorVflip(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_V_FLIP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

unsigned int is_isp_day = 1;
int IMP_ISP_Tuning_SetISPRunningMode(IMPISPRunningMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DAY_OR_NIGHT;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	is_isp_day = (mode == IMPISP_RUNNING_MODE_DAY) ? 1 : 0;

	set_framesource_changewait_cnt();

	pthread_mutex_unlock(&isp_running_mutex);

	return ret;
}

int IMP_ISP_Tuning_SetISPCustomMode(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_CUST_MODE;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	set_framesource_changewait_cnt();

	pthread_mutex_unlock(&isp_running_mutex);

	return ret;
}

int IMP_ISP_Tuning_GetISPCustomMode(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || pmode == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_CUST_MODE;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		*pmode = def_ctrl.control.value;
	}

	return ret;
}

int IMP_ISP_Tuning_GetISPRunningMode(IMPISPRunningMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || pmode == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DAY_OR_NIGHT;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		*pmode = def_ctrl.control.value;
	}
	return ret;
}

int IMP_ISP_Tuning_Awb_GetRgbCoefft(IMPISPCOEFFTWB *isp_core_rgb_coefft_wb_attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;
	if(ispdev == NULL || tuning == NULL || isp_core_rgb_coefft_wb_attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.value = (unsigned int)isp_core_rgb_coefft_wb_attr;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_RGB_COEFFT_WB_ATTR;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	return ret;
}

int IMP_ISP_Tuning_Awb_SetRgbCoefft(IMPISPCOEFFTWB *isp_core_rgb_coefft_wb_attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;
	if(ispdev == NULL || tuning == NULL || isp_core_rgb_coefft_wb_attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_RGB_COEFFT_WB_ATTR;
	def_ctrl.control.value = (unsigned int)isp_core_rgb_coefft_wb_attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CWF_SHIFT failed!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_AE_GetROI(IMPISPWeight *roi_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || roi_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_ROI;
	def_ctrl.control.value = (unsigned int)roi_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_Ae_SetROI failed!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_AE_SetROI(IMPISPWeight *roi_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || roi_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_ROI;
	def_ctrl.control.value = (unsigned int)roi_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_Ae_SetROI failed!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetGamma(IMPISPGamma *gamma)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_GAMMA_ATTR;
	def_ctrl.control.value = (int)gamma;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_GAMMA_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetGamma(IMPISPGamma *gamma)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_GAMMA_ATTR;
	def_ctrl.control.value = (int)gamma;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_GAMMA_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetExpr(IMPISPExpr *expr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || expr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_EXPR_ATTR;
	def_ctrl.control.value = (unsigned int)expr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_EXPR_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetExpr(IMPISPExpr *expr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || expr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_EXPR_ATTR;
	def_ctrl.control.value = (unsigned int)expr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_EXPR_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAeComp(int comp)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_COMP;
	def_ctrl.control.value = comp;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_COMP!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeComp(int *comp)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || comp == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_COMP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_COMP!\n", __func__, __LINE__);
	}
	*comp = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_GetAeLuma(int *luma)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || luma == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_LUMA;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_LUMA!\n", __func__, __LINE__);
	}
	*luma = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetWB(IMPISPWB *wb)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_ATTR;
	def_ctrl.control.value = (unsigned int)wb;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_WB_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetWB(IMPISPWB *wb)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_ATTR;
	def_ctrl.control.value = (unsigned int)wb;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_WB_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetWB_ALGO(IMPISPAWBAlgo wb_algo)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_ALGO;
	def_ctrl.control.value = wb_algo;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_WB_ATTR!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetWB_Statis(IMPISPWB *wb){
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_STATIS_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret) {
		wb->rgain = def_ctrl.control.value >> 16;
		wb->bgain = def_ctrl.control.value & 0xffff;
	}
	return ret;
}

int IMP_ISP_Tuning_GetWB_GOL_Statis(IMPISPWB *wb){
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_STATIS_GOL_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret) {
		wb->rgain = def_ctrl.control.value >> 16;
		wb->bgain = def_ctrl.control.value & 0xffff;
	}

	return ret;
}

int IMP_ISP_Tuning_SetMaxAgain(unsigned int gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_AGAIN_ATTR;
	def_ctrl.control.value = gain;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_AGAIN_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetMaxAgain(unsigned int *gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || gain == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_AGAIN_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_AGAIN_ATTR!\n", __func__, __LINE__);
	}
	*gain = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_SetMaxDgain(unsigned int gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_DGAIN_ATTR;
	def_ctrl.control.value = gain;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_DGAIN_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetMaxDgain(unsigned int *gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || gain == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_DGAIN_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_DGAIN_ATTR!\n", __func__, __LINE__);
	}
	*gain = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_SetHiLightDepress(unsigned int strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH;
	def_ctrl.control.value = strength;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetHiLightDepress(unsigned int *strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || strength == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
	}
	*strength = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetBacklightComp(unsigned int strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH;
	def_ctrl.control.value = strength;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetBacklightComp(unsigned int *strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || strength == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH!\n", __func__, __LINE__);
	}
	*strength = def_ctrl.control.value;

	return ret;
}


int IMP_ISP_Tuning_SetVideoDrop(void (*cb)(void))
{
	IMPISPDev *ispdev = get_ispdev();

	if(ispdev == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	isp_tuning_func_video_drop_set(cb);
//	isp_tuning_deamon_func_add("videodrop", isp_tuning_func_video_drop, NULL);
	return 0;
}

struct isp_table_info {
	unsigned int id;
	unsigned int rows;
	unsigned int cols;
	unsigned int width;
	unsigned int tsource;
	void *ptr;
};

static int isp_table_tuning_ratio(unsigned int talbe_id, unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	struct isp_table_info tinfo;
	int ret = 0;
	int i = 0;
	void *data = 0;
	unsigned int width = 0;
	unsigned int cols = 0;
	unsigned int rows = 0;
	unsigned int size = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	memset(&tinfo, 0, sizeof(tinfo));
	tinfo.id = talbe_id;
	tinfo.tsource = 1;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_TABLE_ATTR;
	def_ctrl.control.value = (unsigned int)&tinfo;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
		return -1;
	}
	cols = tinfo.cols;
	rows = tinfo.rows;
	width = tinfo.width;
	size = cols*rows*width;
	if(0 == size) {
		IMP_LOG_ERR(TAG, "size error !\n");
		return -1;
	}
	data = malloc(size);
	if(data == NULL) {
		IMP_LOG_ERR(TAG, "malloc error !\n");
		return -1;
	}
	tinfo.tsource = 1;
	tinfo.ptr = data;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
		free(data);
		return -1;
	}
#if 0
	printf("##### tinfo.id = %d\n", tinfo.id);
	printf("##### tinfo.rows = %d\n", tinfo.rows);
	printf("##### tinfo.cols = %d\n", tinfo.cols);
	printf("##### tinfo.width = %d\n", tinfo.width);
	printf("##### tinfo.ptr = 0x%x\n", (unsigned int)tinfo.ptr);
	for (i = 0; i < cols*rows; i++) {
		unsigned short *v = tinfo.ptr;
		printf(" %d ", v[i]);
	}
	printf("##### \n");
#endif
	for (i = 0; i < rows; i++) {
		if (1 == width) {
			uint8_t *v = data;
			v[i*2+1] = v[i*2+1]*ratio*1.0/100;
		} else if (2 == width) {
			uint16_t *v = data;
			v[i*2+1] = v[i*2+1]*ratio*1.0/100;
		} else if (4 == width) {
			uint32_t *v = data;
			v[i*2+1] = v[i*2+1]*ratio*1.0/100;
		} else {
			IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
			free(data);
			return -1;
		}
	}
	tinfo.tsource = 0;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
		free(data);
		return -1;
	}
	free(data);
	return ret;
}

#define CALIBRATION_TEMPER_STRENGTH  0x00000084

int IMP_ISP_Tuning_EnableDRC(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_ENABLE_DRC;
	def_ctrl.control.value = (unsigned int)mode;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_ENABLE_DRC!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_EnableDefog(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_ENABLE_DEFOG;
	def_ctrl.control.value = (unsigned int)mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_ENABLE_DEFOG!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAeFreeze(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_FREEZE;
	def_ctrl.control.value = (unsigned int)mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_AE_FREEZE!\n", __func__, __LINE__);
	}
	return ret;
}
int IMP_ISP_Tuning_SetTemperStrength(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_3DNS_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_3DNS_RATIO!\n", __func__, __LINE__);
	}
	return ret;
}

#define CALIBRATION_SINTER_STRENGTH_LINEAR  0x0000006D
int IMP_ISP_Tuning_SetSinterStrength(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_2DNS_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_2DNS_RATIO!\n", __func__, __LINE__);
	}
	return ret;
}

struct isp_core_ev_attr {
	unsigned int ev;
	unsigned int expr_us;
	unsigned int ev_log2;
	unsigned int again;
	unsigned int dgain;
	unsigned int gain_log2;
};

int IMP_ISP_Tuning_GetEVAttr(IMPISPEVAttr *attr)
{

	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	struct isp_core_ev_attr ev_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_EV_ATTR;
	def_ctrl.control.value = (unsigned int)&ev_attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_ISP_EV_ATTR!\n", __func__, __LINE__);
	}
	attr->ev = ev_attr.ev;
	attr->expr_us = ev_attr.expr_us;
	attr->ev_log2 = ev_attr.ev_log2;
	attr->again = ev_attr.again;
	attr->dgain = ev_attr.dgain;
	attr->gain_log2 = ev_attr.gain_log2;

	return ret;
}

struct isp_frame_done_info {
	unsigned int timeout;
	uint64_t cnt;
	int ret;
};

int IMP_ISP_Tuning_WaitFrame(IMPISPWaitFrameAttr *attr)
{

	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	struct isp_frame_done_info fd_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	fd_attr.timeout = attr->timeout;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_WAIT_FRAME_ATTR;
	def_ctrl.control.value = (unsigned int)&fd_attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_ISP_EV_ATTR!\n", __func__, __LINE__);
	}
	attr->cnt = fd_attr.cnt;

	return ret;
}

int IMP_ISP_Tuning_SetAntiFlickerAttr(IMPISPAntiflickerAttr attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int tmp = 0;
	int ret = 0;

	if(tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	if(tuning->antiflicker[0] == attr)
		return 0;
	switch(attr){
	case IMPISP_ANTIFLICKER_DISABLE:
		tmp = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED;
		break;
	case IMPISP_ANTIFLICKER_50HZ:
		tmp = V4L2_CID_POWER_LINE_FREQUENCY_50HZ;
		break;
	case IMPISP_ANTIFLICKER_60HZ:
		tmp = V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
		break;
	default:
		return -1;
	}
	control.id = V4L2_CID_POWER_LINE_FREQUENCY;
	control.value = tmp;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
	if(!ret){
		tuning->antiflicker[0] = attr;
	}
	return ret;
}

int IMP_ISP_Tuning_GetAntiFlickerAttr(IMPISPAntiflickerAttr *pattr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int tmp = 0;
	int ret = 0;

	if(pattr == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	control.id = V4L2_CID_POWER_LINE_FREQUENCY;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL, &control);
	if(!ret){
		switch(control.value){
		case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
			tmp = IMPISP_ANTIFLICKER_DISABLE;
			break;
		case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
			tmp = IMPISP_ANTIFLICKER_50HZ;
			break;
		case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
			tmp = IMPISP_ANTIFLICKER_60HZ;
			break;
		default:
			return -1;
		}
		tuning->antiflicker[0] = tmp;
		*pattr = tmp;
	}
	return ret;
}

int IMP_ISP_Tuning_SetISPProcess(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	if(tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(tuning->isp_process != mode){
		control.id = IMAGE_TUNING_CID_CUSTOM_ISP_PROCESS;
		control.value = mode;
		ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
		if(!ret){
			tuning->isp_process = mode;
		}
	}

	return ret;
}


int IMP_ISP_Tuning_SetShading(int en)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	if(tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	{
		control.id = IMAGE_TUNING_CID_CUSTOM_SHAD;
		control.value = en;
		ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
		printf("##### %s,%s,%d en = %d, ret = %d\n",
		       __FILE__, __func__, __LINE__, en, ret);
	}

	return ret;
}

int IMP_ISP_Tuning_SetFWFreeze(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	if(tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(tuning->fw_freeze != mode){
		control.id = IMAGE_TUNING_CID_CUSTOM_ISP_FREEZE;
		control.value = mode;
		ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL, &control);
		if(!ret){
			tuning->fw_freeze = mode;
		}
	}

	return ret;
}

int IMP_ISP_Tuning_GetAFMetrices(unsigned int *metric)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_METRIC;
	def_ctrl.control.value = (uint32_t)metric;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_EnableMovestate(void)
{
	int ret = 0;
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	struct isp_core_stab_attr stab_attr;
	unsigned int totalgain = 0;
	unsigned int curgain = 0;
	IMPISPRunningMode pmode;
	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);
	ret = IMP_ISP_Tuning_GetISPRunningMode(&pmode);
	if(ret){
		IMP_LOG_ERR(TAG, "%s(%d),IMP_ISP_Tuning_GetISPRunningMode is failed!\n", __func__, __LINE__);
		gmovestate.settingmode = -1;
		goto done;
	}

	gmovestate.settingmode = pmode;
	memset(&stab_attr, 0, sizeof(stab_attr));
	memset(&gmovestate.setting_stab, 0, sizeof(stab_attr));
	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SYSTEM_TAB;
	def_ctrl.control.value = (unsigned int)(&gmovestate.setting_stab);
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d)\n", __func__, __LINE__);
		goto done;
	}

	if(pmode == IMPISP_RUNNING_MODE_DAY){
		stab_attr.stab.global_manual_temper = 1;
#if 0
		if(gmovestate.setting_stab.stab.global_integration_time < 150){
			stab_attr.stab.global_max_integration_time = (unsigned int)gmovestate.setting_stab.stab.global_integration_time * 5 / 6;
		}else if(gmovestate.setting_stab.stab.global_integration_time < 300){
			stab_attr.stab.global_max_integration_time = 150 + 15 * log(gmovestate.setting_stab.stab.global_integration_time - 150);
		}else{
			stab_attr.stab.global_max_integration_time = (unsigned int)gmovestate.setting_stab.stab.global_integration_time * 4 / 5;
		}
#else
		stab_attr.stab.global_max_integration_time = (unsigned int)gmovestate.setting_stab.stab.global_max_integration_time * 3 / 5;
#endif
		stab_attr.stab_ctrl.ctrl_global_max_integration_time = 1;
		totalgain = gmovestate.setting_stab.stab.global_max_sensor_analog_gain;
		totalgain = totalgain * gmovestate.setting_stab.stab.global_max_isp_digital_gain;
		curgain = gmovestate.setting_stab.stab.global_sensor_analog_gain;
		curgain = curgain * gmovestate.setting_stab.stab.global_isp_digital_gain;

		stab_attr.stab.global_manual_sinter = 1;
		stab_attr.stab_ctrl.ctrl_global_manual_sinter = 1;

		stab_attr.stab.global_sinter_threshold_target = curgain / (totalgain / 30) + 1;
		stab_attr.stab_ctrl.ctrl_global_sinter_threshold_target = 1;

		stab_attr.stab.global_temper_threshold_target = gmovestate.temper_target_day;

		stab_attr.stab.global_maximum_sinter_strength = gmovestate.sinter_max_day;
		stab_attr.stab.global_minimum_sinter_strength = gmovestate.sinter_min_day;

		stab_attr.stab.global_maximum_directional_sharpening = gmovestate.sharp_d_max_day;
		stab_attr.stab.global_minimum_directional_sharpening = gmovestate.sharp_d_min_day;

		stab_attr.stab.global_maximum_un_directional_sharpening = gmovestate.sharp_ud_max_day;
		stab_attr.stab.global_minimum_un_directional_sharpening = gmovestate.sharp_ud_min_day;
	}else{
		stab_attr.stab.global_manual_temper = 1;
		stab_attr.stab.global_temper_threshold_target = gmovestate.temper_target_night;

		stab_attr.stab.global_maximum_sinter_strength = gmovestate.sinter_max_night;
		stab_attr.stab.global_minimum_sinter_strength = gmovestate.sinter_min_night;

		stab_attr.stab.global_maximum_directional_sharpening = gmovestate.sharp_d_max_night;
		stab_attr.stab.global_minimum_directional_sharpening = gmovestate.sharp_d_min_night;

		stab_attr.stab.global_maximum_un_directional_sharpening = gmovestate.sharp_ud_max_night;
		stab_attr.stab.global_minimum_un_directional_sharpening = gmovestate.sharp_ud_min_night;
	}
	stab_attr.stab_ctrl.ctrl_global_manual_temper = 1;
	stab_attr.stab_ctrl.ctrl_global_temper_threshold_target = 1;
	stab_attr.stab_ctrl.ctrl_global_maximum_sinter_strength = 1;
	stab_attr.stab_ctrl.ctrl_global_minimum_sinter_strength = 1;
	stab_attr.stab_ctrl.ctrl_global_maximum_directional_sharpening = 1;
	stab_attr.stab_ctrl.ctrl_global_minimum_directional_sharpening = 1;
	stab_attr.stab_ctrl.ctrl_global_maximum_un_directional_sharpening = 1;
	stab_attr.stab_ctrl.ctrl_global_minimum_un_directional_sharpening = 1;

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SYSTEM_TAB;
	def_ctrl.control.value = (unsigned int)(&stab_attr);
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d)\n", __func__, __LINE__);
	}

done:
	pthread_mutex_unlock(&isp_running_mutex);
	return 0;
}

int IMP_ISP_Tuning_DisableMovestate(void)
{
	int ret = 0;
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPRunningMode pmode;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);
	ret = IMP_ISP_Tuning_GetISPRunningMode(&pmode);
	if(ret){
		IMP_LOG_ERR(TAG, "%s(%d),IMP_ISP_Tuning_GetISPRunningMode is failed!\n", __func__, __LINE__);
		goto done;
	}

	if(gmovestate.settingmode != pmode)
		goto done;

	gmovestate.setting_stab.stab_ctrl.ctrl_global_manual_temper = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_max_integration_time = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_manual_sinter = 1;
//	gmovestate.setting_stab.stab_ctrl.ctrl_global_manual_integration_time = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_temper_threshold_target = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_maximum_sinter_strength = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_minimum_sinter_strength = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_maximum_directional_sharpening = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_minimum_directional_sharpening = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_maximum_un_directional_sharpening = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_minimum_un_directional_sharpening = 1;
	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SYSTEM_TAB;
	def_ctrl.control.value = (unsigned int)(&gmovestate.setting_stab);
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d)\n", __func__, __LINE__);
	}

done:
	pthread_mutex_unlock(&isp_running_mutex);
	IMP_Encoder_RequestIDR(0);
	return 0;
}

int IMP_ISP_Tuning_SetAeWeight(IMPISPWeight *ae_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_WEIGHT;
	def_ctrl.control.value = (unsigned int)ae_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeWeight(IMPISPWeight *ae_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_WEIGHT;
	def_ctrl.control.value = (unsigned int)ae_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAfWeight(IMPISPWeight *af_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_WEIGHT;
	def_ctrl.control.value = (unsigned int)af_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAfWeight(IMPISPWeight *af_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_WEIGHT;
	def_ctrl.control.value = (unsigned int)af_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAwbWeight(IMPISPWeight *awb_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_WEIGHT;
	def_ctrl.control.value = (unsigned int)awb_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAwbWeight(IMPISPWeight *awb_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_WEIGHT;
	def_ctrl.control.value = (unsigned int)awb_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAwbClust(IMPISPAWBCluster *awb_cluster)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_cluster == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CLUSTER;
	def_ctrl.control.value = (unsigned int)awb_cluster;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CLUSTER!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAwbClust(IMPISPAWBCluster *awb_cluster)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_cluster == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CLUSTER;
	def_ctrl.control.value = (unsigned int)awb_cluster;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CLUSTER!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAwbCtTrend(IMPISPAWBCtTrend *ct_trend)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT_TREND;
	def_ctrl.control.value = (unsigned int)ct_trend;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CT_TREND!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAwbCtTrend(IMPISPAWBCtTrend *ct_trend)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT_TREND;
	def_ctrl.control.value = (unsigned int)ct_trend;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CT_TREND!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAeTargetList(IMPISPAETargetList *target_list)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_TARGET_LIST;
	def_ctrl.control.value = (unsigned int)target_list;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_TARGET_LIST!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAeTargetList(IMPISPAETargetList *target_list)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_TARGET_LIST;
	def_ctrl.control.value = (unsigned int)target_list;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_TARGET_LIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAeHist(IMPISPAEHist *ae_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_HIST;
	def_ctrl.control.value = (unsigned int)ae_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeHist(IMPISPAEHist *ae_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_HIST;
	def_ctrl.control.value = (unsigned int)ae_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_HIST!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAeHist_Origin(IMPISPAEHistOrigin *ae_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_HIST_ORIGIN;
	def_ctrl.control.value = (unsigned int)ae_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_HIST_ORIGIN!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetAwbHist(IMPISPAWBHist *awb_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_HIST;
	def_ctrl.control.value = (unsigned int)awb_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAwbHist(IMPISPAWBHist *awb_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_HIST;
	def_ctrl.control.value = (unsigned int)awb_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAwbZone(IMPISPAWBZone *awb_zone)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_zone == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_ZONE;
	def_ctrl.control.value = (unsigned int)awb_zone;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_ZONE!\n", __func__, __LINE__);
	}
	return ret;
}
int IMP_ISP_Tuning_SetAfHist(IMPISPAFHist *af_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_HIST;
	def_ctrl.control.value = (unsigned int)af_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAfHist(IMPISPAFHist *af_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_HIST;
	def_ctrl.control.value = (unsigned int)af_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetNCUInfo(IMPISPNcuInfo *ncu_info)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ncu_info == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_GET_NCU_INFO;
	def_ctrl.control.value = (unsigned int)ncu_info;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_HIST!\n", __func__, __LINE__);
	}

	return ret;
}


int IMP_ISP_Tuning_SetAeMin(IMPISPAEMin *ae_min)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_min == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MIN;
	def_ctrl.control.value = (unsigned int)ae_min;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetAeMin!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeMin(IMPISPAEMin *ae_min)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_min == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MIN;
	def_ctrl.control.value = (unsigned int)ae_min;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAeMin!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAe_IT_MAX(unsigned int it_max)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_IT_MAX;
	def_ctrl.control.value = it_max;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_IT_MAX!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAE_IT_MAX(unsigned int *it_max)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_IT_MAX;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_AE_IT_MAX!\n", __func__, __LINE__);
	}
	*it_max = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_GetAeZone(IMPISPZone *ae_zone)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_zone == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_ZONE;
	def_ctrl.control.value = (unsigned int)ae_zone;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_ZONE!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeState(IMPISPAEState *ae_state)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_state == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_STATE;
	def_ctrl.control.value = (unsigned int)ae_state;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_STATE!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAfZone(IMPISPZone *af_zone)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_zone == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_ZONE;
	def_ctrl.control.value = (unsigned int)af_zone;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_ZONE!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetModuleControl(IMPISPModuleCtl *ispmodule)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispmodule == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MODULE_CONTROL;
	def_ctrl.control.value = (unsigned int)ispmodule;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetModuleControl!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetModuleControl(IMPISPModuleCtl *ispmodule)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispmodule == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MODULE_CONTROL;
	def_ctrl.control.value = (unsigned int)ispmodule;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAeMin!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetFrontCrop(IMPISPFrontCrop *ispfrontcrop)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispfrontcrop == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_FCROP_CONTROL;
	def_ctrl.control.value = (unsigned int)ispfrontcrop;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetModuleControl!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetFrontCrop(IMPISPFrontCrop *ispfrontcrop)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispfrontcrop == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_FCROP_CONTROL;
	def_ctrl.control.value = (unsigned int)ispfrontcrop;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAeMin!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetAutoZoom(IMPISPAutoZoom *ispautozoom)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispautozoom == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AUTOZOOM_CONTROL;
	def_ctrl.control.value = (unsigned int)ispautozoom;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetAutoZoom!\n", __func__, __LINE__);
	}

	return ret;
}
int IMP_ISP_Tuning_GetAutoZoom(IMPISPAutoZoom *ispautozoom)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispautozoom == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AUTOZOOM_CONTROL;
	def_ctrl.control.value = (unsigned int)ispautozoom;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAeMin!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetDPC_Strength(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DPC_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DPC_RATIO!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetDPC_Strength(unsigned int *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DPC_RATIO;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DPC_RATIO!\n", __func__, __LINE__);
	}
	*ratio = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetDRC_Strength(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRC_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DRC_RATIO!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetDRC_Strength(unsigned int *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRC_RATIO;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DRC_RATIO!\n", __func__, __LINE__);
	}
	*ratio = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetBcshHue(unsigned char hue)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_BCSH_HUE;
	def_ctrl.control.value = hue;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BCSH_HUE!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetBcshHue(unsigned char *hue)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || hue == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_BCSH_HUE;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BCSH_HUE!\n", __func__, __LINE__);
	}
	*hue = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetHVFLIP(IMPISPHVFLIP hvflip)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(hvflip >= IMPISP_FLIP_MODE_BUTT){
		IMP_LOG_ERR(TAG, "FLIP Can not support this Mode!\n");
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_HV_FLIP;
	def_ctrl.control.value = hvflip;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetHVFlip(IMPISPHVFLIP *hvflip)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_HV_FLIP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*hvflip = def_ctrl.control.value;

	return ret;
}

static void IMP_ISP_Tuning_DumpMask_old(IMPISPMASKAttr *mask)
{
	uint8_t i = 0, j = 0;
	uint8_t tmp[3];
	IMPISP_MASK_BLOCK_PAR *pch[3];

	pch[0] = mask->chn0;
	pch[1] = mask->chn1;
	pch[2] = mask->chn2;
	for(i=0; i<3; i++)
		for (j=0; j<4; j++) {
			memcpy(tmp, &(pch[i][j].mask_value), sizeof(tmp));
			//IMP_LOG_DBG(TAG, "Red:%d\tGreen:%d\tBlue:%d\n", tmp[0], tmp[1], tmp[2]);
			/* ITU-BT.601-7 */
			pch[i][j].mask_value.mask_ayuv.y_value =  0.299	   * tmp[0] + 0.587    * tmp[1] + 0.114    * tmp[2];
			pch[i][j].mask_value.mask_ayuv.u_value = -0.168736 * tmp[0] - 0.331264 * tmp[1] + 0.500    * tmp[2] + 128;
			pch[i][j].mask_value.mask_ayuv.v_value =  0.500    * tmp[0] - 0.418688 * tmp[1] - 0.081312 * tmp[2] + 128;
			/* ITU-BT.709-5 */
			//pch[i][j].mask_value.mask_ayuv.y_value =  0.2126   * tmp[0] + 0.7152   * tmp[1] + 0.0722   * tmp[2];
			//pch[i][j].mask_value.mask_ayuv.v_value = -0.114572 * tmp[0] - 0.385428 * tmp[1] + 0.5      * tmp[2] + 128;
			//pch[i][j].mask_value.mask_ayuv.u_value =  0.5      * tmp[0] - 0.454153 * tmp[1] - 0.045847 * tmp[2] + 128;
			//IMP_LOG_DBG(TAG, "Y:%d\tU:%d\tV:%d\n", pch[i][j].mask_value.mask_ayuv.y_value, pch[i][j].mask_value.mask_ayuv.u_value, pch[i][j].mask_value.mask_ayuv.v_value);
		}
}

int IMP_ISP_Tuning_SetMask(IMPISPMASKAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(mask->mask_type == IMPISP_MASK_TYPE_RGB)
		IMP_ISP_Tuning_DumpMask_old(mask);

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_CONTROL;
	def_ctrl.control.value = (unsigned int)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetMask!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetMask(IMPISPMASKAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || mask == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_CONTROL;
	def_ctrl.control.value = (unsigned int)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetMask!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetScalerLv(IMPISPScalerLv *scaler_level)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SCALER_LEVEL;
	def_ctrl.control.value = (int)scaler_level;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetScalerLv!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetSensorAttr(IMPISPSENSORAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_ATTR_CONTROL;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetSensorAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetAwbCt(unsigned int *ct)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT;
	def_ctrl.control.value = (unsigned int)ct;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetAwbCt!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAWBCt(unsigned int *ct)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ct == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT;
	def_ctrl.control.value = (unsigned int)ct;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAwbCT!\n", __func__, __LINE__);
	}

	return ret;
}

typedef struct {
	char ManualEn;    /* CCM Manual enable ctrl */
	char SatEn;       /* CCM Saturation enable ctrl */
	int ColorMatrix[9];               /* color matrix on manual mode */
} isp_ccm_attr;
int IMP_ISP_Tuning_SetCCMAttr(IMPISPCCMAttr *ccm)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ccm_attr ccm_attr;
	int ret = 0;
	int i = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	for(i = 0; i < 9; i++) {
		if (ccm->ColorMatrix[i] < -0.00001){
			ccm->ColorMatrix[i] = -ccm->ColorMatrix[i];
			ccm_attr.ColorMatrix[i] = (((~((int)(ccm->ColorMatrix[i] * 1024))) + 1) | 0x2000) & 0x3fff;
		} else
			ccm_attr.ColorMatrix[i] = (int)(ccm->ColorMatrix[i] * 1024) & 0x1fff;
	}

	ccm_attr.ManualEn = ccm->ManualEn;
	ccm_attr.SatEn = ccm->SatEn;

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_CCM_ATTR;
	def_ctrl.control.value = (unsigned int)(&ccm_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetCCMAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetCCMAttr(IMPISPCCMAttr *ccm)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ccm_attr ccm_attr;
	int ret = 0;
	int i = 0;

	if(ispdev == NULL || tuning == NULL || ccm == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_CCM_ATTR;
	def_ctrl.control.value = (unsigned int)(&ccm_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	for(i = 0; i < 9; i++) {
		if ((ccm_attr.ColorMatrix[i]) & 0x2000)
			ccm->ColorMatrix[i] = -(float)((((~(ccm_attr.ColorMatrix[i])) + 1) & 0x1fff)) / 1024;
		else
			ccm->ColorMatrix[i] = (float)(ccm_attr.ColorMatrix[i]) / 1024;
	}
	ccm->ManualEn = ccm_attr.ManualEn;
	ccm->SatEn = ccm_attr.SatEn;

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetCCMAttr!\n", __func__, __LINE__);
	}

	return ret;
}

typedef struct {
	uint32_t tisp_ae_manual;
	uint32_t tisp_ae_sensor_again;
	uint32_t tisp_ae_sensor_dgain;
	uint32_t tisp_ae_sensor_integration_time;
	uint32_t tisp_ae_isp_dgian;
	uint32_t tisp_ae_max_sensor_again;
	uint32_t tisp_ae_max_sensor_dgain;
	uint32_t tisp_ae_max_sensor_integration_time;
	uint32_t tisp_ae_max_isp_dgain;
	uint32_t tisp_ae_ev;
	uint32_t tisp_ae_tgain_db;
	uint32_t tisp_ae_again_db;
	uint32_t tisp_ae_ir;
	uint32_t tisp_ae_it_manual;
	uint32_t tisp_ae_ag_manual;
	uint32_t tisp_ae_dg_manual;
	uint32_t tisp_ae_it_short_manual;
	uint32_t tisp_ae_ag_short_manual;
	uint32_t tisp_ae_sensor_again_short;
	uint32_t tisp_ae_sensor_integration_time_short;
	uint32_t tisp_ae_max_sensor_again_short;
	uint32_t tisp_ae_max_sensor_integration_time_short;
	uint32_t tisp_ae_max_sensor_dgain_short;
	uint32_t tisp_ae_max_isp_dgain_short;
	uint32_t tisp_ae_manual_short;
	uint32_t tisp_ae_sensor_dgain_short;
	uint32_t tisp_ae_dg_manual_short;
	uint32_t tisp_ae_isp_dgian_short;
	uint32_t tisp_ae_min_sensor_again;
	uint32_t tisp_ae_min_sensor_dgain;
	uint32_t tisp_ae_min_sensor_integration_time;
	uint32_t tisp_ae_min_isp_dgain;
	uint32_t tisp_ae_min_sensor_again_short;
	uint32_t tisp_ae_min_sensor_integration_time_short;
	uint32_t tisp_ae_min_sensor_dgain_short;
	uint32_t tisp_ae_min_isp_dgain_short;
	uint32_t tisp_ae_sensor_dg_manual;
	uint32_t tisp_ae_sensor_dg_short_manual;
} isp_ae_attr;

int IMP_ISP_Tuning_SetAeAttr(IMPISPAEAttr *ae)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ae_attr ae_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	ae_attr.tisp_ae_manual = ae->AeFreezenEn;
	ae_attr.tisp_ae_it_manual = ae->AeItManualEn;
	ae_attr.tisp_ae_ag_manual = ae->AeAGainManualEn;
	ae_attr.tisp_ae_sensor_dg_manual = ae->AeDGainManualEn;
	ae_attr.tisp_ae_dg_manual = ae->AeIspDGainManualEn;
	ae_attr.tisp_ae_sensor_integration_time = ae->AeIt;
	ae_attr.tisp_ae_sensor_again = ae->AeAGain;
	ae_attr.tisp_ae_sensor_dgain = ae->AeDGain;
	ae_attr.tisp_ae_isp_dgian = ae->AeIspDGain;

	ae_attr.tisp_ae_manual_short = 0;
	ae_attr.tisp_ae_it_short_manual = 0;
	ae_attr.tisp_ae_ag_short_manual = 0;
	ae_attr.tisp_ae_sensor_dg_short_manual = 0;
	ae_attr.tisp_ae_dg_manual_short = 0;
	ae_attr.tisp_ae_sensor_integration_time_short = 0;
	ae_attr.tisp_ae_sensor_again_short = 0;
	ae_attr.tisp_ae_sensor_dgain_short = 0;
	ae_attr.tisp_ae_isp_dgian_short = 0;

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MANUAL_ATTR;
	def_ctrl.control.value = (unsigned int)(&ae_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetAeAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAeAttr(IMPISPAEAttr *ae)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ae_attr ae_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MANUAL_ATTR;
	def_ctrl.control.value = (unsigned int)(&ae_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	ae->AeFreezenEn = ae_attr.tisp_ae_manual;
	ae->AeItManualEn = ae_attr.tisp_ae_it_manual;
	ae->AeAGainManualEn = ae_attr.tisp_ae_ag_manual;
	ae->AeDGainManualEn = ae_attr.tisp_ae_sensor_dg_manual;
	ae->AeIspDGainManualEn = ae_attr.tisp_ae_dg_manual;
	ae->AeIt = ae_attr.tisp_ae_sensor_integration_time;
	ae->AeAGain = ae_attr.tisp_ae_sensor_again;
	ae->AeDGain = ae_attr.tisp_ae_sensor_dgain;
	ae->AeIspDGain = ae_attr.tisp_ae_isp_dgian;

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetAeAttr!\n", __func__, __LINE__);
	}

	return ret;
}

static void *impisp_algo_thread1(void *p)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPAeAlgoFunc *AeAlgoFuncSelf = &ispdev->ae_func;
	IMPISPAEInfoSelf ae_info;
	IMPISPAeAttrSelf ae_result;

	memset(&ae_info, 0, sizeof(IMPISPAEInfoSelf));
	memset(&ae_result, 0, sizeof(IMPISPAeAttrSelf));
	ae_info.version = 210604;
	ae_result.version = 210604;

	while (1) {
		pthread_testcancel();
		if (ioctl(ispdev->vi_fd, TISP_VIDIOC_GET_AE_ALGO_HANDLE, &ae_info)) {
			IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_GET_AE_ALGO_HANDLE error!\n", __func__, __LINE__);
			return NULL;
		}

		AeAlgoFuncSelf->handle(priv_data_self, &ae_info.Aeinfo, &ae_result.AeAttr);
		if(ae_result.AeAttr.AeIntegrationTimeUnit == ISP_CORE_EXPR_UNIT_US){
			ae_result.AeAttr.AeIntegrationTime = ae_result.AeAttr.AeIntegrationTime / (1000000 * (ae_info.Aeinfo.sensor_attr.fps & 0xffff) / ae_info.Aeinfo.sensor_attr.vts * (ae_info.Aeinfo.sensor_attr.fps >> 16));
			ae_result.AeAttr.AeShortIntegrationTime = ae_result.AeAttr.AeShortIntegrationTime / (1000000 * (ae_info.Aeinfo.sensor_attr.fps & 0xffff) / ae_info.Aeinfo.sensor_attr.vts * (ae_info.Aeinfo.sensor_attr.fps >> 16));
		}
		if (ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_AE_ALGO_HANDLE, &ae_result)) {
			IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_SET_AE_ALGO_HANDLE error!\n", __func__, __LINE__);
			return NULL;
		}
	}

	return NULL;
}

void IMP_ISP_SetAeAlgoFunc_close(void)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPAeAlgoFunc *AeAlgoFuncSelf = &ispdev->ae_func;
	struct tx_isp_initarg init_arg;

	pthread_cancel(ae_algo);//取消线程
	pthread_join(ae_algo, NULL);

	if (ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_AE_ALGO_CLOSE, &init_arg)) {
 		IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_SET_AE_ALGO_CLOSE error!\n", __func__, __LINE__);
		return;
	}

	ae_algo_en = 0;
	AeAlgoFuncSelf->close(priv_data_self);

	return;
}

int32_t IMP_ISP_SetAeAlgoFunc(IMPISPAeAlgoFunc *ae_func)
{
	ae_func_tmp = malloc(sizeof(IMPISPAeAlgoFunc));
	memcpy(ae_func_tmp, ae_func, sizeof(IMPISPAeAlgoFunc));
	ae_algo_en = 1;

	return 0;
}

int32_t IMP_ISP_SetAeAlgoFunc_internal(IMPISPAeAlgoFunc *ae_func)
{
	IMPISPDev *ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "[ %s:%d ] ISPDEV cannot open\n", __func__, __LINE__);
		return -1;
	}

	if(gISPdev->vi_state > ISPDEV_STATE_OPEN){
		IMP_LOG_ERR(TAG, "[ %s:%d ] Sensor is runing, please Call 'EmuISP_DisableSensor' firstly\n", __func__, __LINE__);
		return -1;
	}

	if(ae_func == NULL){
		IMP_LOG_ERR(TAG, "[ %s:%d ] Ae algo functions has no defintion\n", __func__, __LINE__);
		return -1;
	}

	priv_data_self = ae_func->priv_data;

	ae_init_info.version = 210604;
	if(ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_AE_ALGO_OPEN, &ae_init_info)) {
		IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_SET_AE_ALGO_OPEN error!\n", __func__, __LINE__);
		return -1;
	}

	ae_func->open(&priv_data_self, &ae_init_info.AeInitAttr);
	pthread_create(&ae_algo, NULL, impisp_algo_thread1, NULL);

	memcpy(&ispdev->ae_func, ae_func, sizeof(IMPISPAeAlgoFunc));
	free(ae_func_tmp);

	return 0;
}

static void *impisp_algo_thread5(void *p)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPAwbAlgoFunc *AwbAlgoFuncSelf = &ispdev->awb_func;
	IMPISPAWBInfoSelf awb_info;
	IMPISPAwbAttrSelf awb_result;

	memset(&awb_info, 0, sizeof(IMPISPAWBInfoSelf));
	memset(&awb_result, 0, sizeof(IMPISPAwbAttrSelf));

	awb_info.version = 210604;
	awb_result.version = 210604;

	while (1) {
		pthread_testcancel();
		if (ioctl(ispdev->vi_fd, TISP_VIDIOC_GET_AWB_ALGO_HANDLE, &awb_info)) {
			IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_GET_AWB_ALGO_HANDLE error!\n", __func__, __LINE__);
			return NULL;
		}

		AwbAlgoFuncSelf->handle(awb_priv_data_self, &awb_info.Awbinfo, &awb_result.AwbAttr);
		if (ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_AWB_ALGO_HANDLE, &awb_result)) {
			IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_SET_AWB_ALGO_HANDLE error!\n", __func__, __LINE__);
			return NULL;
		}
	}

	return NULL;
}

void IMP_ISP_SetAwbAlgoFunc_close(void)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPAwbAlgoFunc *AwbAlgoFuncSelf = &ispdev->awb_func;
	struct tx_isp_initarg init_arg;

	pthread_cancel(awb_algo);//取消线程
	pthread_join(awb_algo, NULL);

	if (ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_AWB_ALGO_CLOSE, &init_arg)) {
 		IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_SET_AWB_ALGO_CLOSE error!\n", __func__, __LINE__);
		return;
	}

	awb_algo_en = 0;

	AwbAlgoFuncSelf->close(awb_priv_data_self);
}

int32_t IMP_ISP_SetAwbAlgoFunc(IMPISPAwbAlgoFunc *awb_func)
{
	awb_func_tmp = malloc(sizeof(IMPISPAwbAlgoFunc));
	memcpy(awb_func_tmp, awb_func, sizeof(IMPISPAwbAlgoFunc));
	awb_algo_en = 1;

	return 0;
}

int32_t IMP_ISP_SetAwbAlgoFunc_internal(IMPISPAwbAlgoFunc *awb_func)
{
	IMPISPDev *ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "[ %s:%d ] ISPDEV cannot open\n", __func__, __LINE__);
		return -1;
	}

	if(gISPdev->vi_state > ISPDEV_STATE_OPEN){
		IMP_LOG_ERR(TAG, "[ %s:%d ] Sensor is runing, please Call 'EmuISP_DisableSensor' firstly\n", __func__, __LINE__);
		return -1;
	}

	if(awb_func == NULL){
		IMP_LOG_ERR(TAG, "[ %s:%d ] Awb algo functions has no defintion\n", __func__, __LINE__);
		return -1;
	}

	awb_priv_data_self = awb_func->priv_data;

	if(ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_AWB_ALGO_OPEN, NULL)) {
		IMP_LOG_ERR(TAG, "[ %s:%d ] TISP_VIDIOC_SET_AWB_ALGO_OPEN error!\n", __func__, __LINE__);
		return -1;
	}

	awb_func->open(awb_priv_data_self);
	pthread_create(&awb_algo, NULL, impisp_algo_thread5, NULL);

	memcpy(&ispdev->awb_func, awb_func, sizeof(IMPISPAwbAlgoFunc));
	free(awb_func_tmp);

	return 0;
}

int IMP_ISP_Tuning_GetBlcAttr(IMPISPBlcAttr *blc)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || blc == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_BLC_ATTR;
	def_ctrl.control.value = (unsigned int)blc;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetBlcAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SetDefog_Strength(uint8_t *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ratio == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DEFOG_STRENGTH;
	def_ctrl.control.value = (unsigned int)ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetDefog_Strength!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetDefog_Strength(uint8_t *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ratio == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DEFOG_STRENGTH;
	def_ctrl.control.value = (unsigned int)ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetDefog_Strength!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SetCsc_Attr(IMPISPCscAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_CSC_ATTR;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetCsc_Attr!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetCsc_Attr(IMPISPCscAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_CSC_ATTR;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetCsc_Attr!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_SetFrameDrop(IMPISPFrameDropAttr *attr)
{
        IMPISPDev *ispdev = get_ispdev();
        int ret = 0;

        if(ispdev == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
        }

	ret = ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_FRAME_DROP, attr);
	if (!ret)
		IMP_LOG_ERR(TAG, "%s(%d),ioctl TISP_VIDIOC_SET_FRAME_DROP!\n", __func__, __LINE__);

        return ret;
}

int32_t IMP_ISP_GetFrameDrop(IMPISPFrameDropAttr *attr)
{
        IMPISPDev *ispdev = get_ispdev();
        int ret = 0;

        if(ispdev == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
        }

	ret = ioctl(ispdev->vi_fd, TISP_VIDIOC_GET_FRAME_DROP, attr);
	if (!ret)
		IMP_LOG_ERR(TAG, "%s(%d),ioctl TISP_VIDIOC_GET_FRAME_DROP!\n", __func__, __LINE__);

        return ret;
}

int32_t IMP_ISP_SetFixedContraster(IMPISPFixedContrastAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_BCSH_SET_FIXED_CONTRAST;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_BCSH_SET_FIXED_CONTRAST!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_SET_GPIO_INIT_OR_FREE(IMPISPGPIO *attr)
{
	int ret = 0;

	IMPISPDev *ispdev = get_ispdev();
	if(ispdev == NULL ||  attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	ret = ioctl(ispdev->vi_fd, VIDIOC_SET_GPIO_INIT, attr);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d)ioctl VIDIOC_GPIO_INIT() error!\n",__func__,__LINE__);
		return -1;
	}

    return 0;

}

int32_t IMP_ISP_SET_GPIO_STA(IMPISPGPIO *attr)
{
	int ret = 0;

	IMPISPDev *ispdev = get_ispdev();
	if(ispdev == NULL ||  attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	ret = ioctl(ispdev->vi_fd, VIDIOC_SET_GPIO_STA, attr);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl VIDIOC_GPIO_STA() error!\n",__func__,__LINE__);
		return -1;
	}

    return 0;

}

static void IMP_ISP_Tuning_DumpMask(IMP_ISP_COLOR_VALUE *value)
{
	uint8_t tmp[3];

	memcpy(tmp, &(value->argb), sizeof(tmp));
	/* IMP_LOG_DBG(TAG, "Red:%d\tGreen:%d\tBlue:%d\n", tmp[0], tmp[1], tmp[2]); */
	/* printf("Red:%d\tGreen:%d\tBlue:%d\n", tmp[0], tmp[1], tmp[2]); */
	/* ITU-BT.601-7 */
	value->ayuv.y_value =  0.299    * tmp[0] + 0.587    * tmp[1] + 0.114    * tmp[2];
	value->ayuv.u_value = -0.168736 * tmp[0] - 0.331264 * tmp[1] + 0.500    * tmp[2] + 128;
	value->ayuv.v_value =  0.500    * tmp[0] - 0.418688 * tmp[1] - 0.081312 * tmp[2] + 128;
	/* ITU-BT.709-5 */
	//value->ayuv.y_value =  0.2126   * tmp[0] + 0.7152   * tmp[1] + 0.0722   * tmp[2];
	//value->ayuv.v_value = -0.114572 * tmp[0] - 0.385428 * tmp[1] + 0.5      * tmp[2] + 128;
	//value->ayuv.u_value =  0.5      * tmp[0] - 0.454153 * tmp[1] - 0.045847 * tmp[2] + 128;
	/* IMP_LOG_DBG(TAG, "Y:%d\tU:%d\tV:%d\n", value->ayuv.y_value, */
	/* 			value->ayuv.u_value, value->ayuv.v_value); */
}

int32_t IMP_ISP_Tuning_SetDrawBlock(IMPISPDrawBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	{
		int enable = 0;
		IMP_ISP_COLOR_VALUE *color;
		switch(attr->type) {
		case IMP_ISP_DRAW_LINE:
			enable = attr->cfg.line.enable;
			color = &attr->cfg.line.color;
			break;
		case IMP_ISP_DRAW_RANGE:
			enable = attr->cfg.rang.enable;
			color = &attr->cfg.rang.color;
			break;
		case IMP_ISP_DRAW_WIND:
			enable = attr->cfg.wind.enable;
			color = &attr->cfg.wind.color;
			break;
		}
		if(enable && attr->color_type == IMPISP_MASK_TYPE_RGB)
			IMP_ISP_Tuning_DumpMask(color);
	}
	memcpy(&tuning->draw_attr[0], attr, sizeof(IMPISPDrawBlockAttr));

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRAW_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_DRAW_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetDrawBlock(IMPISPDrawBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.vinum = 0;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRAW_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_DRAW_ATTR!\n");
	}

	{
		IMP_ISP_COLOR_VALUE *src = &tuning->draw_attr[0][attr->pinum].cfg.line.color;
		IMP_ISP_COLOR_VALUE *color = &attr->cfg.line.color;
		switch(attr->type) {
		case IMP_ISP_DRAW_LINE:
			src = &tuning->draw_attr[0][attr->pinum].cfg.line.color;
			color = &attr->cfg.line.color;
			break;
		case IMP_ISP_DRAW_RANGE:
			src = &tuning->draw_attr[0][attr->pinum].cfg.rang.color;
			color = &attr->cfg.rang.color;
			break;
		case IMP_ISP_DRAW_WIND:
			src = &tuning->draw_attr[0][attr->pinum].cfg.wind.color;
			color = &attr->cfg.wind.color;
			break;
		}

		attr->color_type = tuning->draw_attr[0][attr->pinum].color_type;
		memcpy(&color->argb, &src->argb, sizeof(src->argb));
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetOSDAttr(IMPISPOSDAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_OSD_ATTR!\n");
	}
	return ret;
}

int32_t IMP_ISP_Tuning_SetOSDAttr(IMPISPOSDAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	/* if(pattr->mode < IMPISP_ANTIFLICKER_DISABLE_MODE || pattr->mode >= IMPISP_ANTIFLICKER_BUTT) { */
	/*      IMP_ERROR_RETURN(TAG,EVALOVERFLOW,"MSG: Antiflicker mode(%d) overflow!\n",pattr->mode);*/
	/* } */

	/* if(pattr->mode != IMPISP_ANTIFLICKER_DISABLE_MODE) { */
	/* 	if(pattr->freq < 0 || pattr->freq > 255) */
	/*      IMP_ERROR_RETURN(TAG,EVALOVERFLOW,"MSG: ntiflicker freq(%d) overflow!\n",pattr->freq);*/
	/* } */

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_OSD_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetOSDBlock(IMPISPOSDBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_OSD_BLOCK_ATTR!\n");
	}
	return ret;
}

int32_t IMP_ISP_Tuning_SetOSDBlock(IMPISPOSDBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG,EISPTUNCMD,"MSG: ioctl IMAGE_TUNING_CID_OSD_BLOCK_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SetMaskBlock(IMPISPMaskBlockAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(mask->mask_en && mask->mask_type == IMPISP_MASK_TYPE_RGB)
		IMP_ISP_Tuning_DumpMask(&(mask->mask_value));

	memcpy(&(tuning->mask_attr[0][mask->chx * mask->pinum]), mask, sizeof(IMPISPMaskBlockAttr));

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_BLOCK_ATTR;
	def_ctrl.control.value = (uint32_t)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_MASK_BLOCK_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetMaskBlock(IMPISPMaskBlockAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_BLOCK_ATTR;
	def_ctrl.control.value = (uint32_t)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_MASK_BLOCK_ATTR!\n");
	}

	mask->mask_type = tuning->mask_attr[0][mask->chx * mask->pinum].mask_type;
	mask->mask_value.argb.r_value
		= tuning->mask_attr[0][mask->chx * mask->pinum].mask_value.argb.r_value;
	mask->mask_value.argb.g_value
		= tuning->mask_attr[0][mask->chx * mask->pinum].mask_value.argb.g_value;
	mask->mask_value.argb.b_value
		= tuning->mask_attr[0][mask->chx * mask->pinum].mask_value.argb.b_value;

	return ret;
}













































int IMP_ISP_Tuning_SetSensorFPS_Sec(uint32_t fps_num, uint32_t fps_den)
{
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPAeAlgoFunc *AeAlgoFuncSelf = NULL;
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev *tuning = get_isptuningdev();
	int tmp = (fps_num << 16) | fps_den;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	AeAlgoFuncSelf = &ispdev->ae_func;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_CONTROL_FPS;
	def_ctrl.control.value = tmp;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		tuning->fps_num = fps_num;
		tuning->fps_den = fps_den;
		set_framesource_fps(fps_num, fps_den);
		if(ae_algo_en == 1)
			AeAlgoFuncSelf->notify(priv_data_self, IMPISP_AE_NOTIFY_FPS_CHANGE, NULL);
	}

	return ret;
}

int IMP_ISP_Tuning_GetSensorFPS_Sec(uint32_t *fps_num, uint32_t *fps_den)
{
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPTuningDev * tuning = get_isptuningdev();
	IMPISPDev *ispdev;
	int tmp = 0;
	int ret = 0;

	ispdev = get_ispdev();
	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_CONTROL_FPS;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		tmp = def_ctrl.control.value;
		tuning->fps_num = (tmp >> 16) & 0xffff;
		tuning->fps_den = tmp & 0xffff;
		set_framesource_fps(tuning->fps_num, tuning->fps_den);
		*fps_num = tuning->fps_num;
		*fps_den = tuning->fps_den;
	}

	return ret;
}

int IMP_ISP_Tuning_SetAntiFlickerAttr_Sec(IMPISPAntiflickerAttr attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int tmp = 0;
	int ret = 0;

	if(tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	if(tuning->antiflicker[1] == attr)
		return 0;
	switch(attr){
	case IMPISP_ANTIFLICKER_DISABLE:
		tmp = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED;
		break;
	case IMPISP_ANTIFLICKER_50HZ:
		tmp = V4L2_CID_POWER_LINE_FREQUENCY_50HZ;
		break;
	case IMPISP_ANTIFLICKER_60HZ:
		tmp = V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
		break;
	default:
		return -1;
	}
	control.id = V4L2_CID_POWER_LINE_FREQUENCY;
	control.value = tmp;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL + 2, &control);
	if(!ret){
		tuning->antiflicker[1] = attr;
	}
	return ret;
}

int IMP_ISP_Tuning_GetAntiFlickerAttr_Sec(IMPISPAntiflickerAttr *pattr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int tmp = 0;
	int ret = 0;

	if(pattr == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	control.id = V4L2_CID_POWER_LINE_FREQUENCY;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL + 2, &control);
	if(!ret){
		switch(control.value){
		case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
			tmp = IMPISP_ANTIFLICKER_DISABLE;
			break;
		case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
			tmp = IMPISP_ANTIFLICKER_50HZ;
			break;
		case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
			tmp = IMPISP_ANTIFLICKER_60HZ;
			break;
		default:
			return -1;
		}
		tuning->antiflicker[1] = tmp;
		*pattr = tmp;
	}
	return ret;
}

int IMP_ISP_Tuning_SetBrightness_Sec(unsigned char bright)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), bright = %d\n", __func__, __LINE__, bright);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	control.id = V4L2_CID_BRIGHTNESS;
	control.value = bright;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL + 2, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->brightness[1] = bright;
	}
	return ret;
}

int IMP_ISP_Tuning_GetBrightness_Sec(unsigned char *pbright)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	if(pbright == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	control.id = V4L2_CID_BRIGHTNESS;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL + 2, &control);
	if(!ret){
		*pbright = control.value;
		tuning->brightness[1] = control.value;
	};
	return ret;
}

int IMP_ISP_Tuning_SetContrast_internal_Sec(unsigned char contrast, DEMOSAIC_ISP_MODE_E mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	unsigned char cat;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), contrast=%d, mode = %d\n", __func__, __LINE__, contrast, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if (ispdev->tuning_state != ISPDEV_STATE_RUN) {
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	if (DEMOSAIC_ISP_MODE_CUSTOM == mode){
		custom_contrast[1] = contrast;
		if (DEMOSAIC_ISP_MODE_ENABLE == global_mode)
			return 0;
		cat = custom_contrast[1];
	} else if (DEMOSAIC_ISP_MODE_ENABLE == mode) {
		global_mode = DEMOSAIC_ISP_MODE_ENABLE;
		cat = contrast;
	} else if (DEMOSAIC_ISP_MODE_DISABLE == mode) {
		cat = custom_contrast[1];
		global_mode = DEMOSAIC_ISP_MODE_CUSTOM;
	} else {
		IMP_LOG_ERR(TAG, "%s(%d), We do not support this mode\n", __func__, __LINE__);
		return -1;
	}

	control.id = V4L2_CID_CONTRAST;
	control.value = cat;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL + 2, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->contrast[1] = cat;
	}
	return ret;
}

int IMP_ISP_Tuning_SetContrast_Sec(unsigned char contrast)
{
	int ret = 0;
	ret = IMP_ISP_Tuning_SetContrast_internal_Sec(contrast, DEMOSAIC_ISP_MODE_CUSTOM);
	return ret;
}

int IMP_ISP_Tuning_GetContrast_Sec(unsigned char *pcontrast)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	/* struct v4l2_control control; */
	int ret = 0;

	if(pcontrast == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
#if 0
	control.id = V4L2_CID_CONTRAST;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL, &control);
	if(!ret){
		*pcontrast = control.value;
	};
#endif
	*pcontrast = custom_contrast[1];
	tuning->contrast[1] = custom_contrast[1];
	return ret;
}

int IMP_ISP_Tuning_SetSharpness_internal_Sec(unsigned char sharpness, DEMOSAIC_ISP_MODE_E mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;
	unsigned char sharp;

	//IMP_LOG_INFO(TAG, "%s(%d), sharpness=%d, mode = %d\n", __func__, __LINE__, sharpness, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	if (DEMOSAIC_ISP_MODE_CUSTOM == mode){
		custom_sharpness[1] = sharpness;
		if (DEMOSAIC_ISP_MODE_ENABLE == global_mode)
			return 0;
		sharp = custom_sharpness[1];
	} else if (DEMOSAIC_ISP_MODE_ENABLE == mode) {
		global_mode = DEMOSAIC_ISP_MODE_ENABLE;
		sharp = sharpness;
	} else if (DEMOSAIC_ISP_MODE_DISABLE == mode) {
		sharp = custom_sharpness[1];
		global_mode = DEMOSAIC_ISP_MODE_CUSTOM;
	} else {
		printf("We do not support this mode\n");
		return -1;
	}

	control.id = V4L2_CID_SHARPNESS;
	control.value = sharp;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL + 2, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->sharpness[1] = sharp;
	}

	return ret;
}

int IMP_ISP_Tuning_SetSharpness_Sec(unsigned char sharpness)
{
	int ret = 0;

	ret = IMP_ISP_Tuning_SetSharpness_internal_Sec(sharpness, DEMOSAIC_ISP_MODE_CUSTOM);
	return ret;
}

int IMP_ISP_Tuning_GetSharpness_Sec(unsigned char *psharpness)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	/* struct v4l2_control control; */
	int ret = 0;

	if(psharpness == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

#if 0
	control.id = V4L2_CID_SHARPNESS;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL, &control);
	if(!ret){
		*psharpness = control.value;
	};
#endif
	*psharpness = custom_sharpness[1];
	tuning->sharpness[1] = custom_sharpness[1];

	return ret;
}

int IMP_ISP_Tuning_SetSaturation_Sec(unsigned char sat)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), sat = %d\n", __func__, __LINE__, sat);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	control.id = V4L2_CID_SATURATION;
	control.value = sat;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_S_CTRL + 2, &control);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}else{
		tuning->saturation[1] = sat;
	}
	return ret;
}

int IMP_ISP_Tuning_GetSaturation_Sec(unsigned char *psat)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct v4l2_control control;
	int ret = 0;

	if(psat == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	control.id = V4L2_CID_SATURATION;
	control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_G_CTRL + 2, &control);
	if(!ret){
		*psat = control.value;
		tuning->saturation[1] = control.value;
	};
	return ret;
}

int IMP_ISP_Tuning_SetBcshHue_Sec(unsigned char hue)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_BCSH_HUE;
	def_ctrl.control.value = hue;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BCSH_HUE!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetBcshHue_Sec(unsigned char *hue)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || hue == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_BCSH_HUE;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BCSH_HUE!\n", __func__, __LINE__);
	}
	*hue = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_GetTotalGain_Sec(uint32_t *gain)
{
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPTuningDev * tuning = get_isptuningdev();
	IMPISPDev *ispdev;
	int ret = 0;

	ispdev = get_ispdev();
	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_GET_TOTAL_GAIN;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret)
		*gain = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_SetISPHflip_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), mode = %d\n", __func__, __LINE__, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = V4L2_CID_HFLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetISPHflip_Sec(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = V4L2_CID_HFLIP;
	def_ctrl.control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetISPVflip_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	//IMP_LOG_INFO(TAG, "%s(%d), mode = %d\n", __func__, __LINE__, mode);
	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = V4L2_CID_VFLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetISPVflip_Sec(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = V4L2_CID_VFLIP;
	def_ctrl.control.value = -1;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetSensorHflip_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_H_FLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetSensorHflip_Sec(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_H_FLIP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetSensorVflip_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(tuning == NULL) {
		IMP_LOG_ERR(TAG, "%s(%d), tuning is NULL\n", __func__, __LINE__);
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "%s(%d), ispdev->tuning_state is ISPDEV_STATE_RUN\n", __func__, __LINE__);
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_V_FLIP;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if (ret < 0) {
		IMP_LOG_ERR(TAG, "%s(%d), set VIDIOC_S_CTRL failed\n", __func__, __LINE__);
	}

	set_framesource_changewait_cnt();

	return ret;
}

int IMP_ISP_Tuning_GetSensorVflip_Sec(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(pmode == NULL || tuning == NULL)
		return -1;
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_V_FLIP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*pmode = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetISPRunningMode_Sec(IMPISPRunningMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DAY_OR_NIGHT;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	set_framesource_changewait_cnt();

	pthread_mutex_unlock(&isp_running_mutex);

	return ret;
}

int IMP_ISP_Tuning_SetISPCustomMode_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_CUST_MODE;
	def_ctrl.control.value = mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	set_framesource_changewait_cnt();

	pthread_mutex_unlock(&isp_running_mutex);

	return ret;
}

int IMP_ISP_Tuning_GetISPCustomMode_Sec(IMPISPTuningOpsMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || pmode == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_CUST_MODE;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		*pmode = def_ctrl.control.value;
	}

	return ret;
}

int IMP_ISP_Tuning_GetISPRunningMode_Sec(IMPISPRunningMode *pmode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || pmode == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DAY_OR_NIGHT;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret){
		*pmode = def_ctrl.control.value;
	}
	return ret;
}

int IMP_ISP_Tuning_SetGamma_Sec(IMPISPGamma *gamma)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_GAMMA_ATTR;
	def_ctrl.control.value = (int)gamma;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_GAMMA_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetGamma_Sec(IMPISPGamma *gamma)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_GAMMA_ATTR;
	def_ctrl.control.value = (int)gamma;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_GAMMA_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAeComp_Sec(int comp)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_COMP;
	def_ctrl.control.value = comp;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_COMP!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeComp_Sec(int *comp)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || comp == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_COMP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_COMP!\n", __func__, __LINE__);
	}
	*comp = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_GetAeLuma_Sec(int *luma)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || luma == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_LUMA;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_LUMA!\n", __func__, __LINE__);
	}
	*luma = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetWB_Sec(IMPISPWB *wb)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_ATTR;
	def_ctrl.control.value = (unsigned int)wb;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_WB_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetWB_Sec(IMPISPWB *wb)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_ATTR;
	def_ctrl.control.value = (unsigned int)wb;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_WB_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAeFreeze_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_FREEZE;
	def_ctrl.control.value = (unsigned int)mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_AE_FREEZE!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetExpr_Sec(IMPISPExpr *expr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || expr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_EXPR_ATTR;
	def_ctrl.control.value = (unsigned int)expr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_EXPR_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetExpr_Sec(IMPISPExpr *expr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || expr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_EXPR_ATTR;
	def_ctrl.control.value = (unsigned int)expr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_EXPR_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetWB_Statis_Sec(IMPISPWB *wb){
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_STATIS_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret) {
		wb->rgain = def_ctrl.control.value >> 16;
		wb->bgain = def_ctrl.control.value & 0xffff;
	}
	return ret;
}

int IMP_ISP_Tuning_GetWB_GOL_Statis_Sec(IMPISPWB *wb){
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || wb == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_WB_STATIS_GOL_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(!ret) {
		wb->rgain = def_ctrl.control.value >> 16;
		wb->bgain = def_ctrl.control.value & 0xffff;
	}

	return ret;
}

int IMP_ISP_Tuning_SetAwbClust_Sec(IMPISPAWBCluster *awb_cluster)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_cluster == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CLUSTER;
	def_ctrl.control.value = (unsigned int)awb_cluster;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CLUSTER!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAwbClust_Sec(IMPISPAWBCluster *awb_cluster)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_cluster == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CLUSTER;
	def_ctrl.control.value = (unsigned int)awb_cluster;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CLUSTER!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAwbCtTrend_Sec(IMPISPAWBCtTrend *ct_trend)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT_TREND;
	def_ctrl.control.value = (unsigned int)ct_trend;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CT_TREND!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAwbCtTrend_Sec(IMPISPAWBCtTrend *ct_trend)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT_TREND;
	def_ctrl.control.value = (unsigned int)ct_trend;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CT_TREND!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_Awb_GetRgbCoefft_Sec(IMPISPCOEFFTWB *isp_core_rgb_coefft_wb_attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;
	if(ispdev == NULL || tuning == NULL || isp_core_rgb_coefft_wb_attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.value = (unsigned int)isp_core_rgb_coefft_wb_attr;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_RGB_COEFFT_WB_ATTR;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	return ret;
}

int IMP_ISP_Tuning_Awb_SetRgbCoefft_Sec(IMPISPCOEFFTWB *isp_core_rgb_coefft_wb_attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;
	if(ispdev == NULL || tuning == NULL || isp_core_rgb_coefft_wb_attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_RGB_COEFFT_WB_ATTR;
	def_ctrl.control.value = (unsigned int)isp_core_rgb_coefft_wb_attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_CWF_SHIFT failed!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetMaxAgain_Sec(unsigned int gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_AGAIN_ATTR;
	def_ctrl.control.value = gain;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_AGAIN_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetMaxAgain_Sec(unsigned int *gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || gain == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_AGAIN_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_AGAIN_ATTR!\n", __func__, __LINE__);
	}
	*gain = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_SetMaxDgain_Sec(unsigned int gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_DGAIN_ATTR;
	def_ctrl.control.value = gain;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_DGAIN_ATTR!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetMaxDgain_Sec(unsigned int *gain)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || gain == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MAX_DGAIN_ATTR;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_MAX_DGAIN_ATTR!\n", __func__, __LINE__);
	}
	*gain = def_ctrl.control.value;
	return ret;
}

int IMP_ISP_Tuning_SetHiLightDepress_Sec(unsigned int strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH;
	def_ctrl.control.value = strength;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetHiLightDepress_Sec(unsigned int *strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || strength == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
	}
	*strength = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetBacklightComp_Sec(unsigned int strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH;
	def_ctrl.control.value = strength;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetBacklightComp_Sec(unsigned int *strength)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || strength == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_BACKLIGHT_COMP_STRENGTH!\n", __func__, __LINE__);
	}
	*strength = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetTemperStrength_Sec(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_3DNS_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_3DNS_RATIO!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetSinterStrength_Sec(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_2DNS_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_2DNS_RATIO!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetEVAttr_Sec(IMPISPEVAttr *attr)
{

	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	struct isp_core_ev_attr ev_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_EV_ATTR;
	def_ctrl.control.value = (unsigned int)&ev_attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_ISP_EV_ATTR!\n", __func__, __LINE__);
	}
	attr->ev = ev_attr.ev;
	attr->expr_us = ev_attr.expr_us;
	attr->ev_log2 = ev_attr.ev_log2;
	attr->again = ev_attr.again;
	attr->dgain = ev_attr.dgain;
	attr->gain_log2 = ev_attr.gain_log2;

	return ret;
}

int IMP_ISP_Tuning_EnableMovestate_Sec(void)
{
	int ret = 0;
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	struct isp_core_stab_attr stab_attr;
	unsigned int totalgain = 0;
	unsigned int curgain = 0;
	IMPISPRunningMode pmode;
	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);
	ret = IMP_ISP_Tuning_GetISPRunningMode_Sec(&pmode);
	if(ret){
		IMP_LOG_ERR(TAG, "%s(%d),IMP_ISP_Tuning_GetISPRunningMode is failed!\n", __func__, __LINE__);
		gmovestate.settingmode = -1;
		goto done;
	}

	gmovestate.settingmode = pmode;
	memset(&stab_attr, 0, sizeof(stab_attr));
	memset(&gmovestate.setting_stab, 0, sizeof(stab_attr));
	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SYSTEM_TAB;
	def_ctrl.control.value = (unsigned int)(&gmovestate.setting_stab);
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d)\n", __func__, __LINE__);
		goto done;
	}

	if(pmode == IMPISP_RUNNING_MODE_DAY){
		stab_attr.stab.global_manual_temper = 1;
#if 0
		if(gmovestate.setting_stab.stab.global_integration_time < 150){
			stab_attr.stab.global_max_integration_time = (unsigned int)gmovestate.setting_stab.stab.global_integration_time * 5 / 6;
		}else if(gmovestate.setting_stab.stab.global_integration_time < 300){
			stab_attr.stab.global_max_integration_time = 150 + 15 * log(gmovestate.setting_stab.stab.global_integration_time - 150);
		}else{
			stab_attr.stab.global_max_integration_time = (unsigned int)gmovestate.setting_stab.stab.global_integration_time * 4 / 5;
		}
#else
		stab_attr.stab.global_max_integration_time = (unsigned int)gmovestate.setting_stab.stab.global_max_integration_time * 3 / 5;
#endif
		stab_attr.stab_ctrl.ctrl_global_max_integration_time = 1;
		totalgain = gmovestate.setting_stab.stab.global_max_sensor_analog_gain;
		totalgain = totalgain * gmovestate.setting_stab.stab.global_max_isp_digital_gain;
		curgain = gmovestate.setting_stab.stab.global_sensor_analog_gain;
		curgain = curgain * gmovestate.setting_stab.stab.global_isp_digital_gain;

		stab_attr.stab.global_manual_sinter = 1;
		stab_attr.stab_ctrl.ctrl_global_manual_sinter = 1;

		stab_attr.stab.global_sinter_threshold_target = curgain / (totalgain / 30) + 1;
		stab_attr.stab_ctrl.ctrl_global_sinter_threshold_target = 1;

		stab_attr.stab.global_temper_threshold_target = gmovestate.temper_target_day;

		stab_attr.stab.global_maximum_sinter_strength = gmovestate.sinter_max_day;
		stab_attr.stab.global_minimum_sinter_strength = gmovestate.sinter_min_day;

		stab_attr.stab.global_maximum_directional_sharpening = gmovestate.sharp_d_max_day;
		stab_attr.stab.global_minimum_directional_sharpening = gmovestate.sharp_d_min_day;

		stab_attr.stab.global_maximum_un_directional_sharpening = gmovestate.sharp_ud_max_day;
		stab_attr.stab.global_minimum_un_directional_sharpening = gmovestate.sharp_ud_min_day;
	}else{
		stab_attr.stab.global_manual_temper = 1;
		stab_attr.stab.global_temper_threshold_target = gmovestate.temper_target_night;

		stab_attr.stab.global_maximum_sinter_strength = gmovestate.sinter_max_night;
		stab_attr.stab.global_minimum_sinter_strength = gmovestate.sinter_min_night;

		stab_attr.stab.global_maximum_directional_sharpening = gmovestate.sharp_d_max_night;
		stab_attr.stab.global_minimum_directional_sharpening = gmovestate.sharp_d_min_night;

		stab_attr.stab.global_maximum_un_directional_sharpening = gmovestate.sharp_ud_max_night;
		stab_attr.stab.global_minimum_un_directional_sharpening = gmovestate.sharp_ud_min_night;
	}
	stab_attr.stab_ctrl.ctrl_global_manual_temper = 1;
	stab_attr.stab_ctrl.ctrl_global_temper_threshold_target = 1;
	stab_attr.stab_ctrl.ctrl_global_maximum_sinter_strength = 1;
	stab_attr.stab_ctrl.ctrl_global_minimum_sinter_strength = 1;
	stab_attr.stab_ctrl.ctrl_global_maximum_directional_sharpening = 1;
	stab_attr.stab_ctrl.ctrl_global_minimum_directional_sharpening = 1;
	stab_attr.stab_ctrl.ctrl_global_maximum_un_directional_sharpening = 1;
	stab_attr.stab_ctrl.ctrl_global_minimum_un_directional_sharpening = 1;

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SYSTEM_TAB;
	def_ctrl.control.value = (unsigned int)(&stab_attr);
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d)\n", __func__, __LINE__);
	}

done:
	pthread_mutex_unlock(&isp_running_mutex);
	return 0;
}

int IMP_ISP_Tuning_DisableMovestate_Sec(void)
{
	int ret = 0;
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	IMPISPRunningMode pmode;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	pthread_mutex_lock(&isp_running_mutex);
	ret = IMP_ISP_Tuning_GetISPRunningMode_Sec(&pmode);
	if(ret){
		IMP_LOG_ERR(TAG, "%s(%d),IMP_ISP_Tuning_GetISPRunningMode is failed!\n", __func__, __LINE__);
		goto done;
	}

	if(gmovestate.settingmode != pmode)
		goto done;

	gmovestate.setting_stab.stab_ctrl.ctrl_global_manual_temper = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_max_integration_time = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_manual_sinter = 1;
//	gmovestate.setting_stab.stab_ctrl.ctrl_global_manual_integration_time = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_temper_threshold_target = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_maximum_sinter_strength = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_minimum_sinter_strength = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_maximum_directional_sharpening = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_minimum_directional_sharpening = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_maximum_un_directional_sharpening = 1;
	gmovestate.setting_stab.stab_ctrl.ctrl_global_minimum_un_directional_sharpening = 1;
	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SYSTEM_TAB;
	def_ctrl.control.value = (unsigned int)(&gmovestate.setting_stab);
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d)\n", __func__, __LINE__);
	}

done:
	pthread_mutex_unlock(&isp_running_mutex);
	IMP_Encoder_RequestIDR(0);
	return 0;
}

int IMP_ISP_Tuning_SetAeWeight_Sec(IMPISPWeight *ae_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_WEIGHT;
	def_ctrl.control.value = (unsigned int)ae_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeWeight_Sec(IMPISPWeight *ae_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_WEIGHT;
	def_ctrl.control.value = (unsigned int)ae_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAfWeight_Sec(IMPISPWeight *af_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_WEIGHT;
	def_ctrl.control.value = (unsigned int)af_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAfWeight_Sec(IMPISPWeight *af_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_WEIGHT;
	def_ctrl.control.value = (unsigned int)af_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAwbWeight_Sec(IMPISPWeight *awb_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_WEIGHT;
	def_ctrl.control.value = (unsigned int)awb_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAwbWeight_Sec(IMPISPWeight *awb_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_WEIGHT;
	def_ctrl.control.value = (unsigned int)awb_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_WEIGHT!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_AE_GetROI_Sec(IMPISPWeight *roi_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || roi_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_ROI;
	def_ctrl.control.value = (unsigned int)roi_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_Ae_SetROI failed!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_AE_SetROI_Sec(IMPISPWeight *roi_weight)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || roi_weight == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_ROI;
	def_ctrl.control.value = (unsigned int)roi_weight;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_Ae_SetROI failed!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAwbZone_Sec(IMPISPAWBZone *awb_zone)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_zone == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_ZONE;
	def_ctrl.control.value = (unsigned int)awb_zone;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_ZONE!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAfHist_Sec(IMPISPAFHist *af_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_HIST;
	def_ctrl.control.value = (unsigned int)af_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAfHist_Sec(IMPISPAFHist *af_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_HIST;
	def_ctrl.control.value = (unsigned int)af_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetWB_ALGO_Sec(IMPISPAWBAlgo wb_algo)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_ALGO;
	def_ctrl.control.value = wb_algo;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_WB_ATTR!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetAeHist_Sec(IMPISPAEHist *ae_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_HIST;
	def_ctrl.control.value = (unsigned int)ae_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeHist_Sec(IMPISPAEHist *ae_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_HIST;
	def_ctrl.control.value = (unsigned int)ae_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_HIST!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAeHist_Origin_Sec(IMPISPAEHistOrigin *ae_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_HIST_ORIGIN;
	def_ctrl.control.value = (unsigned int)ae_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_HIST_ORIGIN!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetAwbHist_Sec(IMPISPAWBHist *awb_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_HIST;
	def_ctrl.control.value = (unsigned int)awb_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAwbHist_Sec(IMPISPAWBHist *awb_hist)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || awb_hist == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_HIST;
	def_ctrl.control.value = (unsigned int)awb_hist;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AWB_HIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAFMetrices_Sec(unsigned int *metric)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_METRIC;
	def_ctrl.control.value = (uint32_t)metric;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HILIGHT_DEPRESS_STRENGTH!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAfZone_Sec(IMPISPZone *af_zone)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || af_zone == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AF_ZONE;
	def_ctrl.control.value = (unsigned int)af_zone;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AF_ZONE!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_WaitFrame_Sec(IMPISPWaitFrameAttr *attr)
{

	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	struct isp_frame_done_info fd_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}
	fd_attr.timeout = attr->timeout;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_ISP_WAIT_FRAME_ATTR;
	def_ctrl.control.value = (unsigned int)&fd_attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_ISP_EV_ATTR!\n", __func__, __LINE__);
	}
	attr->cnt = fd_attr.cnt;

	return ret;
}

int IMP_ISP_Tuning_SetAeMin_Sec(IMPISPAEMin *ae_min)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_min == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MIN;
	def_ctrl.control.value = (unsigned int)ae_min;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetAeMin!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAeMin_Sec(IMPISPAEMin *ae_min)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_min == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MIN;
	def_ctrl.control.value = (unsigned int)ae_min;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAeMin!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAe_IT_MAX_Sec(unsigned int it_max)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_IT_MAX;
	def_ctrl.control.value = it_max;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_IT_MAX!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetAE_IT_MAX_Sec(unsigned int *it_max)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_IT_MAX;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_AE_IT_MAX!\n", __func__, __LINE__);
	}
	*it_max = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_GetAeZone_Sec(IMPISPZone *ae_zone)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_zone == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_ZONE;
	def_ctrl.control.value = (unsigned int)ae_zone;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_ZONE!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAeTargetList_Sec(IMPISPAETargetList *target_list)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_TARGET_LIST;
	def_ctrl.control.value = (unsigned int)target_list;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_TARGET_LIST!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAeTargetList_Sec(IMPISPAETargetList *target_list)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_TARGET_LIST;
	def_ctrl.control.value = (unsigned int)target_list;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_TARGET_LIST!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetModuleControl_Sec(IMPISPModuleCtl *ispmodule)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispmodule == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MODULE_CONTROL;
	def_ctrl.control.value = (unsigned int)ispmodule;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetModuleControl!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetModuleControl_Sec(IMPISPModuleCtl *ispmodule)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispmodule == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MODULE_CONTROL;
	def_ctrl.control.value = (unsigned int)ispmodule;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAeMin!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetFrontCrop_Sec(IMPISPFrontCrop *ispfrontcrop)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispfrontcrop == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_FCROP_CONTROL;
	def_ctrl.control.value = (unsigned int)ispfrontcrop;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetModuleControl!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetFrontCrop_Sec(IMPISPFrontCrop *ispfrontcrop)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispfrontcrop == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_FCROP_CONTROL;
	def_ctrl.control.value = (unsigned int)ispfrontcrop;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAeMin!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetDPC_Strength_Sec(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DPC_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DPC_RATIO!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_GetDPC_Strength_Sec(unsigned int *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DPC_RATIO;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DPC_RATIO!\n", __func__, __LINE__);
	}
	*ratio = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetDRC_Strength_Sec(unsigned int ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRC_RATIO;
	def_ctrl.control.value = ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DRC_RATIO!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetDRC_Strength_Sec(unsigned int *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRC_RATIO;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_DRC_RATIO!\n", __func__, __LINE__);
	}
	*ratio = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetHVFLIP_Sec(IMPISPHVFLIP hvflip)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(hvflip >= IMPISP_FLIP_MODE_BUTT){
		IMP_LOG_ERR(TAG, "FLIP Can not support this Mode!\n");
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_HV_FLIP;
	def_ctrl.control.value = hvflip;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetHVFlip_Sec(IMPISPHVFLIP *hvflip)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	memset(&def_ctrl, 0, sizeof(def_ctrl));
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_HV_FLIP;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_HV_FLIP!\n", __func__, __LINE__);
	}
	*hvflip = def_ctrl.control.value;

	return ret;
}

int IMP_ISP_Tuning_SetMask_Sec(IMPISPMASKAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(mask->mask_type == IMPISP_MASK_TYPE_RGB)
		IMP_ISP_Tuning_DumpMask_old(mask);

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_CONTROL;
	def_ctrl.control.value = (unsigned int)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetMask!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetMask_Sec(IMPISPMASKAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || mask == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_CONTROL;
	def_ctrl.control.value = (unsigned int)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetMask!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetSensorAttr_Sec(IMPISPSENSORAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SENSOR_ATTR_CONTROL;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetSensorAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_EnableDRC_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_ENABLE_DRC;
	def_ctrl.control.value = (unsigned int)mode;
	ret = ioctl(ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_ENABLE_DRC!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_EnableDefog_Sec(IMPISPTuningOpsMode mode)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_ENABLE_DEFOG;
	def_ctrl.control.value = (unsigned int)mode;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_ENABLE_DEFOG!\n", __func__, __LINE__);
	}
	return ret;
}

int IMP_ISP_Tuning_SetAwbCt_Sec(unsigned int *ct)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT;
	def_ctrl.control.value = (unsigned int)ct;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetAwbCt!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAWBCt_Sec(unsigned int *ct)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ct == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AWB_CT;
	def_ctrl.control.value = (unsigned int)ct;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAwbCT!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetCCMAttr_Sec(IMPISPCCMAttr *ccm)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ccm_attr ccm_attr;
	int ret = 0;
	int i = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	for(i = 0; i < 9; i++) {
		if (ccm->ColorMatrix[i] < -0.00001){
			ccm->ColorMatrix[i] = -ccm->ColorMatrix[i];
			ccm_attr.ColorMatrix[i] = (((~((int)(ccm->ColorMatrix[i] * 1024))) + 1) | 0x2000) & 0x3fff;
		} else
			ccm_attr.ColorMatrix[i] = (int)(ccm->ColorMatrix[i] * 1024) & 0x1fff;
	}

	ccm_attr.ManualEn = ccm->ManualEn;
	ccm_attr.SatEn = ccm->SatEn;

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_CCM_ATTR;
	def_ctrl.control.value = (unsigned int)(&ccm_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetCCMAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetCCMAttr_Sec(IMPISPCCMAttr *ccm)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ccm_attr ccm_attr;
	int ret = 0;
	int i = 0;

	if(ispdev == NULL || tuning == NULL || ccm == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_CCM_ATTR;
	def_ctrl.control.value = (unsigned int)(&ccm_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	for(i = 0; i < 9; i++) {
		if ((ccm_attr.ColorMatrix[i]) & 0x2000)
			ccm->ColorMatrix[i] = -(float)((((~(ccm_attr.ColorMatrix[i])) + 1) & 0x1fff)) / 1024;
		else
			ccm->ColorMatrix[i] = (float)(ccm_attr.ColorMatrix[i]) / 1024;
	}
	ccm->ManualEn = ccm_attr.ManualEn;
	ccm->SatEn = ccm_attr.SatEn;

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetCCMAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetAeAttr_Sec(IMPISPAEAttr *ae)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ae_attr ae_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	ae_attr.tisp_ae_manual = ae->AeFreezenEn;
	ae_attr.tisp_ae_it_manual = ae->AeItManualEn;
	ae_attr.tisp_ae_ag_manual = ae->AeAGainManualEn;
	ae_attr.tisp_ae_sensor_dg_manual = ae->AeDGainManualEn;
	ae_attr.tisp_ae_dg_manual = ae->AeIspDGainManualEn;
	ae_attr.tisp_ae_sensor_integration_time = ae->AeIt;
	ae_attr.tisp_ae_sensor_again = ae->AeAGain;
	ae_attr.tisp_ae_sensor_dgain = ae->AeDGain;
	ae_attr.tisp_ae_isp_dgian = ae->AeIspDGain;

	ae_attr.tisp_ae_manual_short = 0;
	ae_attr.tisp_ae_it_short_manual = 0;
	ae_attr.tisp_ae_ag_short_manual = 0;
	ae_attr.tisp_ae_sensor_dg_short_manual = 0;
	ae_attr.tisp_ae_dg_manual_short = 0;
	ae_attr.tisp_ae_sensor_integration_time_short = 0;
	ae_attr.tisp_ae_sensor_again_short = 0;
	ae_attr.tisp_ae_sensor_dgain_short = 0;
	ae_attr.tisp_ae_isp_dgian_short = 0;

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MANUAL_ATTR;
	def_ctrl.control.value = (unsigned int)(&ae_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetAeAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAeAttr_Sec(IMPISPAEAttr *ae)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	isp_ae_attr ae_attr;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_MANUAL_ATTR;
	def_ctrl.control.value = (unsigned int)(&ae_attr);
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	ae->AeFreezenEn = ae_attr.tisp_ae_manual;
	ae->AeItManualEn = ae_attr.tisp_ae_it_manual;
	ae->AeAGainManualEn = ae_attr.tisp_ae_ag_manual;
	ae->AeDGainManualEn = ae_attr.tisp_ae_sensor_dg_manual;
	ae->AeIspDGainManualEn = ae_attr.tisp_ae_dg_manual;
	ae->AeIt = ae_attr.tisp_ae_sensor_integration_time;
	ae->AeAGain = ae_attr.tisp_ae_sensor_again;
	ae->AeDGain = ae_attr.tisp_ae_sensor_dgain;
	ae->AeIspDGain = ae_attr.tisp_ae_isp_dgian;

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetAeAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetAeState_Sec(IMPISPAEState *ae_state)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ae_state == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AE_STATE;
	def_ctrl.control.value = (unsigned int)ae_state;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMAGE_TUNING_CID_AE_STATE!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetScalerLv_Sec(IMPISPScalerLv *scaler_level)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SCALER_LEVEL;
	def_ctrl.control.value = (int)scaler_level;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetScalerLv!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_GetBlcAttr_Sec(IMPISPBlcAttr *blc)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || blc == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_BLC_ATTR;
	def_ctrl.control.value = (unsigned int)blc;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetBlcAttr!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SetDefog_Strength_Sec(uint8_t *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ratio == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DEFOG_STRENGTH;
	def_ctrl.control.value = (unsigned int)ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetDefog_Strength!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetDefog_Strength_Sec(uint8_t *ratio)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ratio == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DEFOG_STRENGTH;
	def_ctrl.control.value = (unsigned int)ratio;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetDefog_Strength!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SetCsc_Attr_Sec(IMPISPCscAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_CSC_ATTR;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_SetCsc_Attr!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetCsc_Attr_Sec(IMPISPCscAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_CSC_ATTR;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMP_ISP_Tuning_GetCsc_Attr!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_SetFixedContraster_Sec(IMPISPFixedContrastAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_BCSH_SET_FIXED_CONTRAST;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);

	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl IMAGE_TUNING_CID_BCSH_SET_FIXED_CONTRAST!\n", __func__, __LINE__);
	}

	return ret;
}

int IMP_ISP_Tuning_SetAutoZoom_Sec(IMPISPAutoZoom *ispautozoom)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;

	if(ispdev == NULL || tuning == NULL || ispautozoom == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_AUTOZOOM_CONTROL;
	def_ctrl.control.value = (unsigned int)ispautozoom;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_SetAutoZoom!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SetDrawBlock_Sec(IMPISPDrawBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	{
		int enable = 0;
		IMP_ISP_COLOR_VALUE *color;
		switch(attr->type) {
		case IMP_ISP_DRAW_LINE:
			enable = attr->cfg.line.enable;
			color = &attr->cfg.line.color;
			break;
		case IMP_ISP_DRAW_RANGE:
			enable = attr->cfg.rang.enable;
			color = &attr->cfg.rang.color;
			break;
		case IMP_ISP_DRAW_WIND:
			enable = attr->cfg.wind.enable;
			color = &attr->cfg.wind.color;
			break;
		}
		if(enable && attr->color_type == IMPISP_MASK_TYPE_RGB)
			IMP_ISP_Tuning_DumpMask(color);
	}
	memcpy(&tuning->draw_attr[0], attr, sizeof(IMPISPDrawBlockAttr));

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRAW_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_DRAW_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetDrawBlock_Sec(IMPISPDrawBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.vinum = 1;
	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.control.id = IMAGE_TUNING_CID_DRAW_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_DRAW_ATTR!\n");
	}

	{
		IMP_ISP_COLOR_VALUE *src = &tuning->draw_attr[1][attr->pinum].cfg.line.color;
		IMP_ISP_COLOR_VALUE *color = &attr->cfg.line.color;
		switch(attr->type) {
		case IMP_ISP_DRAW_LINE:
			src = &tuning->draw_attr[1][attr->pinum].cfg.line.color;
			color = &attr->cfg.line.color;
			break;
		case IMP_ISP_DRAW_RANGE:
			src = &tuning->draw_attr[1][attr->pinum].cfg.rang.color;
			color = &attr->cfg.rang.color;
			break;
		case IMP_ISP_DRAW_WIND:
			src = &tuning->draw_attr[1][attr->pinum].cfg.wind.color;
			color = &attr->cfg.wind.color;
			break;
		}

		attr->color_type = tuning->draw_attr[1][attr->pinum].color_type;
		memcpy(&color->argb, &src->argb, sizeof(src->argb));
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetOSDAttr_Sec(IMPISPOSDAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_OSD_ATTR!\n");
	}
	return ret;
}

int32_t IMP_ISP_Tuning_SetOSDAttr_Sec(IMPISPOSDAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	/* if(pattr->mode < IMPISP_ANTIFLICKER_DISABLE_MODE || pattr->mode >= IMPISP_ANTIFLICKER_BUTT) { */
	/*      IMP_ERROR_RETURN(TAG,EVALOVERFLOW,"MSG: Antiflicker mode(%d) overflow!\n",pattr->mode);*/
	/* } */

	/* if(pattr->mode != IMPISP_ANTIFLICKER_DISABLE_MODE) { */
	/* 	if(pattr->freq < 0 || pattr->freq > 255) */
	/*      IMP_ERROR_RETURN(TAG,EVALOVERFLOW,"MSG: ntiflicker freq(%d) overflow!\n",pattr->freq);*/
	/* } */

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_OSD_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetOSDBlock_Sec(IMPISPOSDBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_OSD_BLOCK_ATTR!\n");
	}
	return ret;
}

int32_t IMP_ISP_Tuning_SetOSDBlock_Sec(IMPISPOSDBlockAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_OSD_BLOCK_ATTR;
	def_ctrl.control.value = (int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG,EISPTUNCMD,"MSG: ioctl IMAGE_TUNING_CID_OSD_BLOCK_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SetMaskBlock_Sec(IMPISPMaskBlockAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	if(mask->mask_en && mask->mask_type == IMPISP_MASK_TYPE_RGB)
		IMP_ISP_Tuning_DumpMask(&(mask->mask_value));

	memcpy(&(tuning->mask_attr[0][mask->chx * mask->pinum]), mask, sizeof(IMPISPMaskBlockAttr));

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_BLOCK_ATTR;
	def_ctrl.control.value = (uint32_t)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_MASK_BLOCK_ATTR!\n");
	}

	return ret;
}

int32_t IMP_ISP_Tuning_GetMaskBlock_Sec(IMPISPMaskBlockAttr *mask)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int32_t ret = 0;

	if(ispdev == NULL || tuning == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}
	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_GET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_MASK_BLOCK_ATTR;
	def_ctrl.control.value = (uint32_t)mask;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_ERROR_RETURN(TAG, EISPTUNCMD, "MSG: ioctl IMAGE_TUNING_CID_MASK_BLOCK_ATTR!\n");
	}

	mask->mask_type = tuning->mask_attr[0][mask->chx * mask->pinum].mask_type;
	mask->mask_value.argb.r_value
		= tuning->mask_attr[0][mask->chx * mask->pinum].mask_value.argb.r_value;
	mask->mask_value.argb.g_value
		= tuning->mask_attr[0][mask->chx * mask->pinum].mask_value.argb.g_value;
	mask->mask_value.argb.b_value
		= tuning->mask_attr[0][mask->chx * mask->pinum].mask_value.argb.b_value;

	return ret;
}

int IMP_ISP_SetSensorRegister_Sec(uint32_t index, uint32_t value)
{
	int ret = 0;
	struct v4l2_dbg_register reg;
	IMPSensorInfo *info = NULL;
	IMPISPDev * ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}

	if(gISPdev->vi_state < ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "Sensor doesn't Run!\n");
		return -1;
	}
	info = &ispdev->active_sensor;
	if(info->cbus_type == 0){
		IMP_LOG_ERR(TAG, "There isn't sensor!\n");
		return -1;
	}
	if(info->cbus_type == TX_SENSOR_CONTROL_INTERFACE_I2C){
		reg.match.type = V4L2_CHIP_MATCH_I2C_DRIVER;
	}else{
		IMP_LOG_ERR(TAG, "Don't support spi sensor!\n");
		return -1;
	}
	memcpy(reg.match.name, &info->name, sizeof(reg.match.name));
	reg.reg = index | (1 << 31);
	reg.val = value;
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_DBG_S_REGISTER,&reg)) !=0 ){
		printf("sorry,g_register failed!\n");
	}
	return ret;
}

int IMP_ISP_GetSensorRegister_Sec(uint32_t index, uint32_t *value)
{
	int ret = 0;
	struct v4l2_dbg_register reg;
	IMPSensorInfo *info = NULL;
	IMPISPDev * ispdev = get_ispdev();

	if(ispdev == NULL){
		IMP_LOG_ERR(TAG, "ISPDEV cannot open\n");
		return -1;
	}

	if(gISPdev->vi_state < ISPDEV_STATE_RUN){
		IMP_LOG_ERR(TAG, "Sensor doesn't Run!\n");
		return -1;
	}
	info = &ispdev->active_sensor;
	if(info->cbus_type == 0){
		IMP_LOG_ERR(TAG, "There isn't sensor!\n");
		return -1;
	}
	if(info->cbus_type == TX_SENSOR_CONTROL_INTERFACE_I2C){
		reg.match.type = V4L2_CHIP_MATCH_I2C_DRIVER;
	}else{
		IMP_LOG_ERR(TAG, "Don't support spi sensor!\n");
		return -1;
	}
	memcpy(reg.match.name, &info->name, sizeof(reg.match.name));
	reg.reg = index | (1 << 31);
	if((ret = ioctl(ispdev->vi_fd, VIDIOC_DBG_G_REGISTER,&reg)) !=0 ){
		printf("sorry,g_register failed!\n");
	}
	*value = (unsigned int)(reg.val);
	return ret;
}

int32_t IMP_ISP_SetFrameDrop_Sec(IMPISPFrameDropAttr *attr)
{
        IMPISPDev *ispdev = get_ispdev();
        int ret = 0;

        if(ispdev == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
        }

	ret = ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_FRAME_DROP1, attr);
	if (!ret)
		IMP_LOG_ERR(TAG, "%s(%d),ioctl TISP_VIDIOC_SET_FRAME_DROP!\n", __func__, __LINE__);

        return ret;
}

int32_t IMP_ISP_GetFrameDrop_Sec(IMPISPFrameDropAttr *attr)
{
        IMPISPDev *ispdev = get_ispdev();
        int ret = 0;

        if(ispdev == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
        }

	ret = ioctl(ispdev->vi_fd, TISP_VIDIOC_GET_FRAME_DROP1, attr);
	if (!ret)
		IMP_LOG_ERR(TAG, "%s(%d),ioctl TISP_VIDIOC_GET_FRAME_DROP!\n", __func__, __LINE__);

        return ret;
}

int32_t IMP_ISP_Tuning_SwitchBin(IMPISPBinAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;
	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 0;
	def_ctrl.control.id = IMAGE_TUNING_CID_SWITCH_BIN;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAwbCT!\n", __func__, __LINE__);
	}

	return ret;
}

int32_t IMP_ISP_Tuning_SwitchBin_Sec(IMPISPBinAttr *attr)
{
	IMPISPDev *ispdev = get_ispdev();
	IMPISPTuningDev * tuning = get_isptuningdev();
	struct isp_image_tuning_default_ctrl def_ctrl;
	int ret = 0;
	if(ispdev == NULL || tuning == NULL || attr == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	if(ispdev->tuning_state != ISPDEV_STATE_RUN){
		return -1;
	}

	def_ctrl.dir = TX_ISP_PRIVATE_IOCTL_SET;
	def_ctrl.vinum = 1;
	def_ctrl.control.id = IMAGE_TUNING_CID_SWITCH_BIN;
	def_ctrl.control.value = (unsigned int)attr;
	ret = ioctl (ispdev->tuning_fd, VIDIOC_DEFAULT_CMD_ISP_TUNING, &def_ctrl);
	if(0 != ret) {
		IMP_LOG_ERR(TAG, "%s(%d),ioctl  IMP_ISP_Tuning_GetAwbCT!\n", __func__, __LINE__);
	}

	return ret;
}


int32_t IMP_ISP_StreamCheck(IMPISPStreamCheck *check)
{
	IMPISPDev *ispdev = get_ispdev();
	int ret = 0;

	if(ispdev == NULL || check == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	ret = ioctl(ispdev->vi_fd, TISP_VIDIOC_STREAM_CHECK, check);
	if (!ret)
		IMP_LOG_ERR(TAG, "%s(%d),ioctl TISP_VIDIOC_STREAM_CHECK!\n", __func__, __LINE__);

	return ret;
}

int32_t IMP_ISP_SetStreamOut(IMPISPStreamState *state)
{
	IMPISPDev *ispdev = get_ispdev();
	int ret = 0;

	if(ispdev == NULL || state == NULL) {
		IMP_LOG_ERR(TAG, "get_ispdev error !\n");
		return -1;
	}

	ret = ioctl(ispdev->vi_fd, TISP_VIDIOC_SET_STREAM_OUT, state);
	if (!ret)
		IMP_LOG_ERR(TAG, "%s(%d),ioctl TISP_VIDIOC_SET_STREAM_OUT!\n", __func__, __LINE__);

	return ret;
}


struct fast_mem_attr{
    uint32_t devmem_fast_vaddr;
    uint32_t devmem_fast_paddr;
    uint32_t devmem_fast_length;
    int devmem_fast_fd;
};
struct fast_mem_node {
    struct fast_mem_attr attr;
    struct fast_mem_node *next;
};

struct fast_mem_node *head = NULL;
struct fast_mem_node* append_node(void) {
    struct fast_mem_node *new_node = (struct node *)malloc(sizeof(struct fast_mem_node));
    memset(new_node, 0, sizeof(struct fast_mem_node));
    new_node->next = NULL;
    if (head == NULL) {
        head = new_node;
    } else {
        struct fast_mem_node *current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_node;
    }
    return new_node;
}

int delete_node(struct fast_mem_attr *pattr,uint32_t paddr) {
    struct fast_mem_node *current = head;
    struct fast_mem_node *prev = NULL;

    while (current != NULL) {
        if((current->attr.devmem_fast_paddr == paddr)){
            break;
        }
        prev = current;
        current = current->next;
    }

    if (current != NULL) {
        if (prev == NULL) {
            head = current->next;
        } else {
            prev->next = current->next;
        }
        memcpy(pattr,&(current->attr),sizeof(struct fast_mem_attr));
        free(current);
        return 0;
    } else {
        printf("Node with addr %x not found\n", paddr);
        return -1;
    }
}
static void dump_fast_list(void)
{
    struct fast_mem_node *current = head;
    while (current != NULL) {
        IMP_LOG_INFO(TAG,"%#x %#x %#x; %d", current->attr.devmem_fast_vaddr,
               current->attr.devmem_fast_paddr,
               current->attr.devmem_fast_length,
               current->attr.devmem_fast_fd);
        current = current->next;
    }
    printf("\n");
}



int32_t IMP_ISP_GetFastFrame(int vinum,IMPIspFastFrameAttr *attr)
{
    int fd = 0;
    int openTimes = 0xfff;
    int chn = -1;
    int ret = 0;

    struct fast_channel_ioctl fc;
    uint32_t nv12_length = 0;
    uint32_t devmem_fast_vaddr = 0;
    uint32_t devmem_fast_paddr = 0;
    uint32_t devmem_fast_length = 0;
    int devmem_fast_fd = 0;
    struct fast_mem_node *mem_node = NULL;

    if(attr == NULL){
        IMP_LOG_ERR(TAG, "[%s %d]input attr is NULL\n",__func__,__LINE__);
        return -1;
    }
    while (((fd = open("/proc/multi_channel", O_RDWR )) < 0) && (openTimes-- > 0)){
        usleep(10000);
    }
    if(!openTimes){
        IMP_LOG_ERR(TAG, "[%s %d]Failed to open\n",__func__,__LINE__);
        return -1;
    }
    if (fd < 0) {
        IMP_LOG_ERR(TAG, "[%s %d]Failed to open\n",__func__,__LINE__);
        return -1;
    }
    memset(&fc, 0 ,sizeof(fc));

    chn = attr->chn;
    nv12_length = attr->width * attr->height *3/2;
    fc.chan_index = chn;
    /*ioctl*/
    ret = ioctl(fd, TISP_VIDIOC_GET_FASTFRAME_PHYADDR , &fc);
    if(ret!=0){
        IMP_LOG_ERR(TAG, "%s: TISP_VIDIOC_DQBUF chaneel%d failed\n", __func__, chn);
        goto err1;
    }
    /*mmap*/
    devmem_fast_fd = open("/dev/rmem", O_RDWR|O_SYNC);
    if(devmem_fast_fd < 0) {
        IMP_LOG_ERR(TAG,"open /dev/rmem faied\n");
        goto err1;
    }
    devmem_fast_paddr  = fc.phy_addr & (~0xf0000000);
    devmem_fast_length = nv12_length;
    devmem_fast_vaddr  = (uint32_t)mmap(NULL, devmem_fast_length, PROT_READ|PROT_WRITE, MAP_SHARED, devmem_fast_fd, devmem_fast_paddr);
    if (MAP_FAILED == (void*)devmem_fast_vaddr) {
        perror("mmap");
        goto err_all;
    }

    attr->viraddr       = devmem_fast_vaddr;
    attr->phyaddr       = devmem_fast_paddr;
    attr->total_length  = nv12_length;

    mem_node = append_node();
    mem_node->attr.devmem_fast_fd        = devmem_fast_fd;
    mem_node->attr.devmem_fast_length    = nv12_length;
    mem_node->attr.devmem_fast_paddr     = devmem_fast_paddr;
    mem_node->attr.devmem_fast_vaddr     = devmem_fast_vaddr;

    dump_fast_list();
    close(fd);
    return 0;
err_all:
    close(devmem_fast_fd);
err1:
    close(fd);
    return -1;
}

int32_t IMP_ISP_ReleseFastFrame(int vinum, IMPIspFastFrameAttr *attr)
{
    int ret = 0;
    if(attr == NULL){
        IMP_LOG_ERR(TAG, "[%s %d]input attr is NULL\n",__func__,__LINE__);
        return -1;
    }
    dump_fast_list();
    struct fast_mem_attr mattr;
    memset(&mattr, 0, sizeof(struct fast_mem_attr));
    if(!delete_node(&mattr, attr->phyaddr)){
        if (munmap((void*)mattr.devmem_fast_vaddr, mattr.devmem_fast_length) == -1){
            IMP_LOG_ERR(TAG, "error:(%s,%d),munmap failed\n",__func__,__LINE__);
            close(mattr.devmem_fast_fd);
            return -1;
        }
        close(mattr.devmem_fast_fd);
    }
    /*ioctl*/
    struct fast_channel_ioctl fc;
    int fd = 0;
    int openTimes = 0xfff;
    fc.chan_index = attr->chn;
    while (((fd = open("/proc/multi_channel", O_RDWR )) < 0) && (openTimes-- > 0)){
        usleep(10000);
    }
    if(!openTimes){
        IMP_LOG_ERR(TAG, "[%s %d]Failed to open\n",__func__,__LINE__);
        return -1;
    }
    if (fd < 0) {
        IMP_LOG_ERR(TAG, "[%s %d]Failed to open\n",__func__,__LINE__);
        return -1;
    }

    ret = ioctl(fd, TISP_VIDIOC_REALSE_FASTFRAME_PHYADDR, &fc);
    if(ret != 0){
        IMP_LOG_ERR(TAG, "%s: TISP_VIDIOC_DQBUF chaneel%d failed\n", __func__,fc.chan_index);
        goto err;
    }
    close(fd);
    dump_fast_list();
    return 0;

err:
    close(fd);
    return -1;
}

#if 0
int IMP_ISP_Tuning_SetISPBypass(IMPISPTuningOpsMode enable);
int IMP_ISP_Tuning_SetISPHflip(IMPISPTuningOpsMode mode);
int IMP_ISP_Tuning_GetISPHflip(IMPISPTuningOpsMode *pmode);
int IMP_ISP_Tuning_SetISPVflip(IMPISPTuningOpsMode mode);
int IMP_ISP_Tuning_GetISPVflip(IMPISPTuningOpsMode *pmode);
int IMP_ISP_Tuning_SetVideoDrop(void (*cb)(void));
int32_t IMP_ISP_SetAeAlgoFunc(IMPISPAeAlgoFunc *ae_func);
int32_t IMP_ISP_SetAwbAlgoFunc(IMPISPAwbAlgoFunc *awb_func);
int32_t IMP_ISP_SET_GPIO_INIT_OR_FREE(IMPISPGPIO *attr);
int32_t IMP_ISP_SET_GPIO_STA(IMPISPGPIO *attr);

int IMP_ISP_SetSensorRegister(uint32_t reg, uint32_t value);
int IMP_ISP_GetSensorRegister(uint32_t reg, uint32_t *value);
int32_t IMP_ISP_SetFrameDrop(IMPISPFrameDropAttr *attr);
int32_t IMP_ISP_GetFrameDrop(IMPISPFrameDropAttr *attr);
int32_t IMP_ISP_Tuning_GetDrawBlock(IMPISPDrawBlockAttr *attr);
#endif

#if 0
int IMP_ISP_OSD_Alloc(uint32_t *addr, uint8_t *saddr, uint32_t len)
{
	IMPAlloc alloc;

	memset(&alloc, 0, sizeof(IMPAlloc));
	IMP_Alloc(&alloc, len, "osd");
	memcpy(alloc.info.vaddr, saddr, len);
	*addr = alloc.info.paddr;

	return 0;
}
#endif
